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

RadGrid- get all data in data table

12 Answers 1050 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ben
Top achievements
Rank 1
Ben asked on 20 Oct 2010, 05:19 PM

Hi,
I am trying to get all items from radgrid to a data table. While I am using this code, shown below, geting error in "foreach (GridViewRow row in grdShippedOrders.Rows)" Rows is not recognizing in code behind file.
grdShippedOrders is my radgrid in aspx page. I want to get all items in a dataset or data table. So that I can view it in another grid in another page.

 

 

 

protected

 

 

DataTable Button1_Click(object sender, EventArgs e)

 

{

 

 

 

DataTable dtRecords = new DataTable();

 

 

 

foreach (DataControlField col in grdShippedOrders.Columns)

 

dtRecords.Columns.Add(

 

new DataColumn(col.HeaderText));

 

 

 

foreach (GridViewRow row in grdShippedOrders.Rows)

 

{

 

 

DataRow dr = dtRecords.NewRow();

 

 

 

foreach (DataControlFieldCell cell in row.Cells)

 

dr[row.Cells.GetCellIndex(cell)] = cell.Text;

dtRecords.Rows.Add(dr);

}

 

 

return dtRecords;

 

}

12 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 21 Oct 2010, 09:06 AM
Hello Ben,

The following code snippet shows how to get all items from RadGrid to a DataTable.

C#:
protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dtRecords = new DataTable();
        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;  
    }

Since you want the Datatable to populate grid in another page, store the DataTable in a session variable.

Thanks,
Princy.
0
Ben
Top achievements
Rank 1
answered on 21 Oct 2010, 01:42 PM
Thanks Princy,

I am not getting data in a new radgrid in an another page. This is my code. Can you find out any thing wrong. I open first page and click the button (Button1_Click) then I can see the radgrid in second page but no data and column headings.

protected void Button1_Click1(object sender, EventArgs e)
        {
            DataTable dtRecords = new DataTable();
        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);
        }
       
            Session["DT"]=dtRecords;
        }

 

This is in my new page code behind.
protected void needdatasource(object source, GridNeedDataSourceEventArgs e)
        {
           DataTable table = (DataTable)Session["DT"];
                       grdVisitPkg.DataSource = table;
                   }

0
Ben
Top achievements
Rank 1
answered on 21 Oct 2010, 04:03 PM
Now its working. Thanks
Can you give me the code to store these data in to a database?
0
siva
Top achievements
Rank 1
answered on 23 Dec 2010, 05:31 PM

Hi,
am not getting value in  row[Col.Uniquename].Text; getting only null value'''''
plz help me in retriving corresponding value from griditem


 

0
Princy
Top achievements
Rank 1
answered on 24 Dec 2010, 06:59 AM
Hello Siva,

The above code will work if its GridBoundColumn. If you are using GridTemplateColumn  it will return empty string. In case of AutoGeneratedColumn, try the following code snippet.

C#:
foreach (GridDataItem row in RadGrid1.Items) // loops through each rows in RadGrid
       {
          foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns) //loops through each AutoGenerated column in RadGrid
           {
               string v = row[col.UniqueName].Text;
           }
       }

Please elaborate your situation if it doesn't help.

Thanks,
Princy.
0
siva
Top achievements
Rank 1
answered on 25 Dec 2010, 12:08 PM
HI princy,

Thanks for your help.

Now i hav another doubt.

i have designed  radgrid in following way

 

<table id="tblUpper" width="100%" cellpadding="0" cellspacing="0" style="table-layout: fixed;">

 

 

<tr>

 

 

<td valign="top">

 

 

<telerik:RadGrid ID="RadGridClientMandate" GridLines="None" runat="server" AutoGenerateColumns="False"

 

 

Skin="3iGrid" EnableEmbeddedSkins="false" Height="390px" Width="99.5%">

 

 

<MasterTableView InsertItemDisplay="Bottom" Width="100%" DataKeyNames="Security"

 

 

Visible="true">

 

 

<RowIndicatorColumn>

 

 

<HeaderStyle Width="20px" />

 

 

</RowIndicatorColumn>

 

 

<ExpandCollapseColumn>

 

 

<HeaderStyle Width="20px" />

 

 

</ExpandCollapseColumn>

 

 

<Columns>

 

 

<telerik:GridTemplateColumn HeaderText="Asset Type" UniqueName="Asset_type" Visible="true">

 

 

<ItemTemplate>

 

 

<asp:Label ID="Asset_type" runat="server" Text='<%# Bind("Asset_type") %>' />

 

 

</ItemTemplate>

 

 

</telerik:GridTemplateColumn>

 

 

</Columns>

 

 

</MasterTableView>

 

 

<ClientSettings>

 

 

<Scrolling AllowScroll="True" />

 

 

<Resizing AllowColumnResize="True" />

 

 

</ClientSettings>

 

 

</telerik:RadGrid>

 

 

</td>

 

 

</tr>

 

 

</table>

 

now i need to  add asp textbox which should only allow numerics, i used telerik: radnumeric but it doesnt mingle with my grid structure or style. PLz help me in this and give alternate validation to enter only numeric
0
Princy
Top achievements
Rank 1
answered on 28 Dec 2010, 08:50 AM
Hello Siva,

In order to achieve this attach 'onkeypress' event to the TextBox and in that event handler add validation to enter only numeric value. Sample code is given below.

ASPX:
<telerik:GridTemplateColumn>
    <ItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           TextBox txtbox = (TextBox)item.FindControl("TextBox1");
           txtbox.Attributes.Add("onkeypress", "return CheckNemericValue(event);");//here textbox1 is ur server side control
       }
   }

Java Script:
<script type="text/javascript">
    function CheckNemericValue(e) {
        var key = e.keyCode;
        if (key >= 48 && key <= 57) {
            return true;
        }
        else {
            alert("please enter number only");
            return false;
        }
    }
 </script>

Thanks,
Princy.
0
Nahwin
Top achievements
Rank 1
answered on 03 May 2013, 08:43 AM

protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dtRecords = new DataTable();
        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;  
    }

Hi Princy,

just want to ask is this code will work if paging is set to true ?
or theres a need for special handling when paging is used ?
0
Nahwin
Top achievements
Rank 1
answered on 03 May 2013, 08:45 AM
double post...
0
Shinu
Top achievements
Rank 1
answered on 06 May 2013, 05:36 AM
Hi,

One suggestion is that you can turn off paging and then loop through rows as shown below.
c#
protected void Button1_Click(object sender, EventArgs e)
{
    RadGrid1.AllowPaging = false;
    RadGrid1.Rebind();
    foreach (GridDataItem item in RadGrid1.Items)
    {
     ...
    }
 RadGrid1.AllowPaging = true;
    RadGrid1.Rebind();
}

Thanks,
Shinu
0
Imran
Top achievements
Rank 1
answered on 24 Sep 2013, 04:39 PM
Hey, 

I am using below
protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dtRecords = new DataTable();
        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;  
    }
for binding radgrid data to datatable but it keep giving me error of unhandled Exception of Column name that CustomerID doesn't belong to table, though it does. Any guidance will be appreciated
Thanks
0
Nahwin
Top achievements
Rank 1
answered on 25 Sep 2013, 03:36 PM
Hi Shinu,

thanks for the suggestion really nice simple idea..


Hi Imran,

I think i've encountered such an error, make sure on "dtRecords" it has column called "CustomerID".

The error is thrown because on grid you've specified a column called "CustomerID" yet on the data source you are trying to bind there is no such colum.


Other possibility is different name between one specified on the grid and one existing in the data source..

Regards
Tags
Grid
Asked by
Ben
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Ben
Top achievements
Rank 1
siva
Top achievements
Rank 1
Nahwin
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
Imran
Top achievements
Rank 1
Share this question
or