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
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.
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;
}
Can you give me the code to store these data in to a database?
Hi,
am not getting value in row[Col.Uniquename].Text; getting only null value'''''
plz help me in retriving corresponding value from griditem
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.
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
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.
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 ?
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
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;
}
Thanks
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