Loop through rows of radgrid and retrieve cell values

2 Answers 7105 Views
Grid
Mike
Top achievements
Rank 1
Mike asked on 13 Jan 2011, 07:39 PM
Prior to radgrid i used to be able to loop through all the rows in my grid using "RowDataBound" and get the cell value using "e.Row.Cells(4).Text" but that option is not available with radrid.  what i need to do is loop through each row of the radgrid and retrieve the cell values in the rows and do validation.  i have grouping, filtering, paging and scroll bars enabled.  any ideas.  i am new to radgrid.

thanks.

2 Answers, 1 is accepted

Sort by
1
Princy
Top achievements
Rank 2
answered on 14 Jan 2011, 06:48 AM
Hello Mike,

You can use ItemDataBound event of RadGrid to get the row and cell value.ItemDataBound event fires for each item in grid.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
  {
     if (e.Item is GridDataItem)
      {
          GridDataItem item = (GridDataItem)e.Item;
          string vaue = item.Cells[4].Text;
      }
  }

Also please go through the following documentation for accessing rows and cells in RadGrid.
Accessing cells and rows

Thanks,
Princy.
Mike
Top achievements
Rank 1
commented on 14 Jun 2011, 11:47 PM

I searched all of your forum and unable to find a solution to my problem.  Here is what i have

I have added a template column for a check box:

1.) aspx code
 <telerik:GridTemplateColumn UniqueName="TemplateColumn" AllowFiltering="false" >
                            <ItemTemplate>
                                <asp:Panel ID="Panel1" runat="server">
                                    <asp:CheckBox ID="chbxSelection" runat="server" AutoPostBack="true" OnCheckedChanged="CheckedChanged" />
                                </asp:Panel>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>

2.) vb cod behind
 Protected Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chkBox As CheckBox = CType(sender, CheckBox)

        Dim myPanel As Panel = CType(chkBox.Parent, Panel)
        Dim dataItem As GridDataItem = CType(myPanel.NamingContainer, GridDataItem)

        If chkBox.Checked Then
            dataItem("Order_Number").Style("color") = "orange"
            TextBox1.Text += "," & dataItem("Rec_ID").Text
        Else
            dataItem("Order_Number").Style("color") = "black"
        End If
    End Sub

My problem is that the textbox1.text value is not populating with the "Rec_ID" cell value from the radgrid.  What i ultimately need to do is get a list of the Rec_ID values for the rows that are selected and have a super hard time.  Your help is greatly appreciated.
0
Mike
Top achievements
Rank 1
answered on 15 Jun 2011, 12:10 AM
I actually just resolved my issue

 Try
            For Each row As GridDataItem In radgvShipDocs.Items
                ' loops through each rows in RadGrid
                'For Each col As GridColumn In radgvShipDocs.MasterTableView.Items
                'loops through each AutoGenerated column in RadGrid
                Dim v As String = row("Order_Number").Text
                Dim v2 As String = row("Rec_ID").Text


                If row("Order_Number").Style("color") = "orange" Then
                    TextBox1.Text += "," & v2
                End If


                'Next
            Next
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
Shawn
Top achievements
Rank 1
commented on 04 Dec 2011, 04:27 PM

Nevermind on this issue, data in table had spaces using TRIM funciton on conditional statement fixed everything, knew better but got burned.


I have struggled with conditional formatting of cell colors based on the value of the cell itself, enabled and disabled skins no luck.   I've tried all the options out there, databind event did not work.  Looping command using for each with IF statemnet does not seem to examine the value at all, it seems to process every condition no matter the value.  I seem to see the same issue of the condtion not being examinied properly with databinding.  Do you see any issues with this code, no matter the value it response writes every statement no matter what.  The correct cell values are posted back and match the RadGrid for every row.


protected void RadButton1_Command(object sender, CommandEventArgs e)
{
Label1.Text = "The Button has been clicked"; 
 
foreach (GridDataItem item in RadGrid1.Items)
{
TableCell cell = item["Status"];
string strtest = (item["Status"].Text);
// string strOnSite = "On-Site";
//string strOffSite = "Off-Site";
Response.Write("This is what for each sees: " + strtest + "<br>");
if (cell.Text == "On-Site")
Response.Write("Value is On-Site: " + strtest + "<br>");

else
Response.Write("Value is not On-Site: " + strtest + "<br>");

 

Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Share this question
or