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

want to populate data in dataset from radgrid

1 Answer 407 Views
Grid
This is a migrated thread and some comments may be shown as answers.
sachin
Top achievements
Rank 1
sachin asked on 20 Oct 2014, 03:19 PM

want to populate data in dataset from radgrid  , i have  tried following eas bit not working. please provide solution ASAP
Source, DataTable)  -Getting datatable null

2)not getting data from ITemTemplates

le();
        foreach (GridColumn col in grdShippedOrders.Columns)
        {
            DataColumn colString = new DataColumn(col.UniqueName);
            dtRecords.Columns.Add(colString);
 
        }
        foreach (GridDataItem row in grdShippedOrders.Items) // loops through each rows in RadGrid
        {
            DataRow dr = dtRecords.NewRow();
            foreach (GridColumn col in grdShippedOrders.Columns) //loops through each column in RadGrid
                   dr[col.UniqueName] = row[col.UniqueName].Text;
            dtRecords.Rows.Add(dr);
        }
      return dtRecords;

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 20 Oct 2014, 07:40 PM
Hello,

Please try with the below code snippet.

DataTable dtRecords = new DataTable();
 
foreach (GridColumn col in RadGrid1.Columns)
{
    DataColumn colString = new DataColumn(col.UniqueName);
    dtRecords.Columns.Add(colString);
 
}
foreach (GridDataItem row in RadGrid1.Items) // loops through each rows in RadGrid
{
    DataRow dr = dtRecords.NewRow();
    foreach (GridColumn col in RadGrid1.Columns) //loops through each column in RadGrid
    {
        if (col.ColumnType == "GridTemplateColumn")
        {
            dr[col.UniqueName] = (row.FindControl("Label1") as Label).Text;
        }
        else
        {
            dr[col.UniqueName] = row[col.UniqueName].Text;
        }
 
    }
    dtRecords.Rows.Add(dr);
}

<MasterTableView AutoGenerateColumns="False" DataKeyNames="Id">
    <Columns>
        <telerik:GridBoundColumn DataField="Id" HeaderText="Id" UniqueName="Id">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
        </telerik:GridBoundColumn>
        <telerik:GridTemplateColumn UniqueName="MyColumn">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
    </Columns>
</MasterTableView>


OR

<MasterTableView AutoGenerateColumns="False" DataKeyNames="Id">
    <Columns>
        <telerik:GridBoundColumn DataField="Id" HeaderText="Id" UniqueName="Id">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
        </telerik:GridBoundColumn>
        <telerik:GridTemplateColumn UniqueName="MyColumn">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
    </Columns>
</MasterTableView>

DataTable dtRecords = new DataTable();
 
foreach (GridColumn col in RadGrid1.Columns)
{
    DataColumn colString = new DataColumn(col.UniqueName);
    dtRecords.Columns.Add(colString);
 
}
foreach (GridDataItem row in RadGrid1.Items) // loops through each rows in RadGrid
{
    DataRow dr = dtRecords.NewRow();
    foreach (GridColumn col in RadGrid1.Columns) //loops through each column in RadGrid
    {
        if (col.UniqueName == "MyColumn")
        {
            dr[col.UniqueName] = (row.FindControl("Label1") as Label).Text;
        }
        else
        {
            dr[col.UniqueName] = row[col.UniqueName].Text;
        }
 
    }
    dtRecords.Rows.Add(dr);
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
sachin
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or