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

Empty records when using DataTable as Datasource

6 Answers 557 Views
Grid
This is a migrated thread and some comments may be shown as answers.
dexter
Top achievements
Rank 1
dexter asked on 29 Feb 2012, 11:22 AM

Hi ,

I can't seem to bind the source from my datatable. It is not showing anything.

protected void grdDmnDocFolder_NeedDataSource(object o, GridNeedDataSourceEventArgs e)
    {
      DataTable dt = new DataTable();
  
  
      dt.Columns.Add("aaa");
      dt.Columns.Add("ccc");
      dt.Columns.Add("bbb");
  
      DataRow dr = dt.NewRow();
      dr["aaa"] = "a";
      dr["bbb"] = "b";
      dr["ccc"] = "c";
  
      dt.Rows.Add(dr);
      dt.Rows.Add(dr);
      dt.Rows.Add(dr);
      dt.Rows.Add(dr);
  
      grdDmnDocFolder.DataSource = dt;
  
    }

On my aspx page, i already set my OnNeedDataSource event as grdDmnDocFolder_NeedDataSource. What i'm trying to do here is just creating dummy records. What am i missing?

Please help.

Thank you.

Regards,
Dexter

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 29 Feb 2012, 11:28 AM
Hello Dexter,

please check this link. In this link i already bind RadGrid with DataTable.

Let me know if any concern.

Thanks,
Jayesh Goyani
0
Gajanan
Top achievements
Rank 2
answered on 29 Feb 2012, 12:20 PM
hi,
try below code

 DataTable dt = new DataTable();
        dt.Columns.Add("aaa");
        dt.Columns.Add("ccc");
        dt.Columns.Add("bbb");

        
        DataRow dr =  dt.NewRow();
        DataRow dr1 = dt.NewRow();
        DataRow dr2 = dt.NewRow();
        dr["aaa"] = "a";
        dr1["bbb"] = "b";
        dr2["ccc"] = "c";

        dt.Rows.Add(dr);
        dt.Rows.Add(dr1);
        dt.Rows.Add(dr2);
        
        RadGrid1.DataSource = dt;
0
dexter
Top achievements
Rank 1
answered on 01 Mar 2012, 06:03 AM
Hi,

Thanks for those who replied. Its working now, the Grid Bound column in my radgrid is what is causing the problem.
However, i do need another solution.

How do i create new rows for the radgrid with GridBoundColumn already declared on the aspx page?

I want to use datatable as the datasource.

Hope to hear from anyone soon. Thanks.

Regards,
Dexter
0
Shinu
Top achievements
Rank 2
answered on 01 Mar 2012, 07:25 AM
Hello,

RadGrid does not support mixing declarative grid columns with grid columns added dynamically at runtime. You should either create all the columns in the grid programmatically, or else define them all in the ASPX file.Check the following help documentation.
Programmatic Creation

-Shinu.
0
dexter
Top achievements
Rank 1
answered on 01 Mar 2012, 07:49 AM
Hi Shinu,

Thanks for your reply. Since i can already create the columns dynamically,
i would like to know how to populate the already defined columns in the aspx using
DataTable as data source.

If you can provide me with a working example(aspx & cs), it would be great.

Thanks,
Dexter
0
Tsvetina
Telerik team
answered on 05 Mar 2012, 06:42 PM
Hello Dexter,

There would be no difference in populating your columns. As long as you assign the DataTable as datasource in NeedDataSource and set the columns DataField property to point to the fields in the datatable, you should be able to see the data.

If this is not so, there could be some problem with the order of creating your programmatic columns. In such case, check out the link that Shinu posted and follow the applicable instructions. For your scenario I assume it will be Dynamically defining the structure of a statically-declared grid.

All the best,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
dexter
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Gajanan
Top achievements
Rank 2
dexter
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Tsvetina
Telerik team
Share this question
or