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

How to loop through a Grid with data

3 Answers 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
johnson lim
Top achievements
Rank 1
johnson lim asked on 03 Jul 2012, 10:12 AM
Hi All,
I have a grid which is perfectly fine and fill with data.
Now i need to loop through the grid in order to read out its column name and data in a button click event.I have search online and try with few  alternatives and it is not working.Can anyone help?
I have try with :

1). ajaxExport.Columns(0).HeaderText

2). For Each column As GridDataItem In ajaxExport.Items
                If TypeOf column Is GridDataItem Then
                    Dim col As GridDataItem = TryCast(column, GridDataItem)

                    a = col(0).Text
                    'col.Width = 90
                    'col.HeaderText = "Column count: " + i.ToString
                    i = i + 1
                End If
            Next


Help Please!!

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 03 Jul 2012, 10:41 AM
Hello Johnson,

Try the following code to achieve your scenario.
VB:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
    For Each col As GridColumn In RadGrid1.Columns
            'accessing columns
        Dim value As String = col.UniqueName.ToString()
    Next
    For Each item As GridDataItem In RadGrid1.Items
            'accessing cell value
        Dim text As String = item("UniqueName").Text
    Next
End Sub

Thanks,
Shinu.
0
johnson lim
Top achievements
Rank 1
answered on 04 Jul 2012, 09:37 AM
Hi Shinu,
1).The code is not working, even i have bind the data to the grid. In the button click event. the column count is still 0.

2).Because the grid is bind dynamically, is it possibel to access via index number?
0
Shinu
Top achievements
Rank 2
answered on 05 Jul 2012, 05:52 AM
Hello Johnson,

I have created the grid in PageInit method and it is working as expected.
C#:
RadGrid RadGrid1;
protected
void Page_Init(object sender, System.EventArgs e)
{
  RadGrid1 = new RadGrid();
  RadGrid1.ID = "RadGrid1";
  PlaceHolder1.Controls.Add(RadGrid1);
  RadGrid1.MasterTableView.AutoGenerateColumns = false;
  GridBoundColumn boundColumn;
  boundColumn = new GridBoundColumn();
  GridBoundColumn boundColumn1 = new GridBoundColumn();
  boundColumn1.DataField = "ContactName";
  boundColumn1.UniqueName = "ConactName";
  boundColumn1.HeaderText = "ConactName";
  RadGrid1.MasterTableView.Columns.Add(boundColumn);
  boundColumn.DataField = "EmployeeID";
  boundColumn.HeaderText = "EmployeeID";
    .....//adding columns
protected void Button1_Click(object sender, EventArgs e)
{
  foreach (GridColumn col in RadGrid1.Columns)
  {
    string value = col.UniqueName.ToString();
  }
}

Thanks,
Shinu.
Tags
Grid
Asked by
johnson lim
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
johnson lim
Top achievements
Rank 1
Share this question
or