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

[Solved] How to access grid rows and columns?

5 Answers 260 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
shahid Aleem
Top achievements
Rank 1
shahid Aleem asked on 08 Feb 2010, 09:16 AM
gridHubSitesGroup.Items[0].Cells[0].Text = "0"
        gridHubSitesGroup.Items[0].Cells[1].Text = sq_str1.ExecuteScalar().ToString(); 
Hello I am trying to get the rows and cells of radgrid.

I want the items to insert into cells from CodeBehind.

This is how i am trying but it is throwing an exception saying Index was out of range.
Please reply.
Thanks.

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 08 Feb 2010, 10:42 AM
Hello Shahid

You can access/set the cell value using ColumnUniqueName as shown below.

CS:
         gridHubSitesGroup.Items[0]["CustomerID"].Text = "my text";       // Where CustomerID is the ColumnUniqueName

You will find more information about accessing cells by going through the following link:
Accessing cells and rows

-Shinu.
0
shahid Aleem
Top achievements
Rank 1
answered on 08 Feb 2010, 10:58 AM
still i am getting the same problem.
and the link u provided in that only if any event of the grid raises then i can do some thing.

But i dont have any grid event in my code behind.

In the normal gridview i can able to do this using

 GridHubGroup.Rows[0].Cells[0].Text = "0";
GridHubGroup.Rows[0].Cells[1].Text = sq_str1.ExecuteScalar().ToString();

i want in this way.

thanks.



0
Princy
Top achievements
Rank 2
answered on 09 Feb 2010, 02:50 PM
Hi,

It is always a better technique to use the UniqueName property to access the cell values in case you do column swapping.You can access  this in a button click event as shown below:

 protected void Button3_Click(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.Items[0]["CustomerID"].Text = "my text";  
        RadGrid1.Rebind();
    }

If yous till need to use the column index you will need to check for the correct column index  which I believe starts from 2 for the columns.

Thanks,
Princy
0
shahid Aleem
Top achievements
Rank 1
answered on 09 Feb 2010, 03:58 PM
Hello princy,

Thanks for your reply.

i tried the way u told me. But still i am getting the same error.

Here is my code.
<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" AllowSorting="True" 
                        AutoGenerateColumns="False" GridLines="None" Width="100%" DataSourceID="SqlHubSitesGroupSource1" OnItemDataBound="gridHubSites_ItemDataBound">  
                        <MasterTableView DataSourceID="SqlHubSitesSource" ShowFooter="True">  
                            <Columns> 
                             
                                <telerik:GridTemplateColumn UniqueName="HubSites">  
                               <ItemTemplate> 
                               </ItemTemplate> 
                               </telerik:GridTemplateColumn> 
                                <telerik:GridBoundColumn UniqueName="TotalHubSites" DataField="TotalHubSites" HeaderText="Total Hub Sites" 
                                    SortExpression="TotalHubSites" FilterControlWidth="50px">  
                                </telerik:GridBoundColumn> 
                               <telerik:GridTemplateColumn UniqueName="View">  
                               <ItemTemplate> 
                               </ItemTemplate> 
                               </telerik:GridTemplateColumn> 
                                 
                            </Columns> 
                        </MasterTableView> 
                    </telerik:RadGrid> 
And in the code behind
private void hubgroupwise()  
{  
string str1 = "select Count(HubSites) as 'TotalHubSites' from HubSites";  
        DBLayer.CustomSQLCommand sq_str1 = new DBLayer.CustomSQLCommand(str1, false);  
RadGrid1.MasterTableView.Items[0]["HubSites"].Text = "0";  
        RadGrid1.MasterTableView.Items[0]["TotalHubSites"] = sq_str1.ExecuteScalar().ToString();  

This is the error i am getting.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

at this line.
 RadGrid1.MasterTableView.Items[0]["HubSites"].Text = "0";

Looking forward for a positive reply.
Thanks,
shahid.

0
Schlurk
Top achievements
Rank 2
answered on 09 Feb 2010, 05:30 PM
From the sound of things you are attempting to access the Items collection before the collection actually exists. Which event are you attempting to change this text? Also, have you tried to change the text within a Button's OnClick event? Using the code you just posted the same setup worked for me (in an OnClick event).
Tags
General Discussions
Asked by
shahid Aleem
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
shahid Aleem
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Schlurk
Top achievements
Rank 2
Share this question
or