This is a migrated thread and some comments may be shown as answers.

On Click Load Grid

3 Answers 190 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nenna
Top achievements
Rank 1
Nenna asked on 18 Nov 2019, 01:20 PM

I have an on click event and I would like to bind to the Grid on click and make it view able.  My code is not working.  The grid is not made view able on button click.

 

My code:

. protected void btnFind_Click(object sender, EventArgs e)
        {
           
            strFind = txtFind.Text;
            AddFileNames(dataDir);
            

            foreach (var fileName in allFiles)
            {
                Document doc = new Document(fileName);
                Regex regex = new Regex(strFind, RegexOptions.IgnoreCase);
                FindReplaceOptions options = new FindReplaceOptions();
                doc.Range.Replace(regex, new ProcessFiles(), false);
                if (doc.HasRevisions == true)
                {
                    //string strFileName = System.IO.Path.GetFileName(fileName);
                    changedFiles.Add(fileName);
                    Session.Add("ChangeFilesList", changedFiles);
                    doc.Save(fileName);
                }

               
            }

            txtFind.Text = string.Empty;

            RadGrid1.DataSource = GetDocuments();


        }

 

private DataTable GetDocuments()
        {
            var dt = new DataTable();

           List<string> fileEntries = Session["allFilesList"] as List<string>;
            string[] files = fileEntries.ToArray();
            
            dt.Columns.Add("ID", typeof(Int32));
            dt.Columns.Add("DirectoryName", typeof(string));
            dt.Columns.Add("FileName", typeof(string));
            dt.Columns.Add("LastModifiedTime", typeof(DateTime));
            dt.Columns.Add("FilePath", typeof(string));
            dt.Columns.Add("BinaryData", typeof(byte[]));

            dt.PrimaryKey = new DataColumn[] { dt.Columns["ID"] };

            for (int i = 0; i < files.Length; i++)
            {
                var dr = dt.NewRow();

                dr["ID"] = i;

                var fileInfo = new FileInfo(fileEntries[i]);

                dr["DirectoryName"] = fileInfo.Directory.Name;
                dr["FileName"] = fileInfo.Name;
                dr["LastModifiedTime"] = fileInfo.LastWriteTime;
                dr["FilePath"] = fileEntries[i];
                dr["BinaryData"] = File.ReadAllBytes(fileEntries[i]);

                dt.Rows.Add(dr);
            }
            return dt;
        }

 

 

3 Answers, 1 is accepted

Sort by
0
Attila Antal
Telerik team
answered on 19 Nov 2019, 03:39 PM

Hi Nenna,

Create a RadGrid and wire up the NeedDataSource event handler. This event handler will be used to bind data to RadGrid when needed. In order to make the RadGrid hidden on initial load, set it's Visible property to false.

<telerik:RadGrid ID="RadGrid3" runat="server" OnNeedDataSource="RadGrid3_NeedDataSource" Visible="false">
</telerik:RadGrid>

C# - NeedDataSource event binding to an example DataSource, you will need to change this to reflect the real data source in your case.

protected void RadGrid3_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    (sender as RadGrid).DataSource = Enumerable.Range(1, 3).Select(x => new
    {
        ID = x,
        Description = "Description " + x
    });
}

 

Once you have the Hidden Grid on the page, let me explain you the steps for changing the Visible property of the RadGrid to True during the click event of a button.

Assuming there is a button called "btnFind" which fires the server-side click event called "btnFind_Click".

Markup

<telerik:RadButton ID="btnFind" runat="server" Text="Find" OnClick="btnFind_Click">
</telerik:RadButton>

C# 

protected void btnFind_Click(object sender, EventArgs e)
{
    // Change the Visible property to true
    RadGrid3.Visible = true;
    // Rebind the Grid for the changes to take over
    RadGrid3.Rebind();
}

 

Kind regards,
Attila Antal
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Nenna
Top achievements
Rank 1
answered on 19 Nov 2019, 04:08 PM

I get the following error when I run the application after applying the code changes:

DataBinding: '<>f__AnonymousType0`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'FileName'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: '<>f__AnonymousType0`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'FileName'.

Source Error:

Line 90: 
Line 91:             txtFind.Text = string.Empty;
Line 92:             RadGrid1.Rebind();
Line 93: 
Line 94: 
0
Attila Antal
Telerik team
answered on 20 Nov 2019, 09:59 AM

Hi Nenna,

When the error message says "... does not contain a property with the name 'FileName'." it means that the DataSource assigned to the RadGrid does not have fields with the name "FileName".

 

The example I have shared below contains a dummy datasource just in case if you would like to have a complete version, but in your case you will need to assign the GetDocuments() as the Grid's datasource.

For Example:

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = GetDocuments();
}

 

Kind regards,
Attila Antal
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Nenna
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Nenna
Top achievements
Rank 1
Share this question
or