Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
grdStateAtten.Rows[0].Cells[0].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[1].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[2].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[3].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[4].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[5].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[6].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[7].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[8].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[9].Text = ds.Tables[0].ToString();
          grdStateAtten.Rows[0].Cells[10].Text = ds.Tables[0].ToString();
          grdStateAtten.DataBind();

Here I display value in gridview from two different table i get error
Quote:
gridview index was out of range. must be non-negative and less than the size of the collection. parameter name: index
Posted
Updated 12-May-15 1:28am
v2
Comments
CHill60 12-May-15 7:29am    
Does the gridview have (at least) 11 columns and (at least) 1 row?
ZurdoDev 12-May-15 8:29am    
This means you are trying to access something that does not exist. What exactly is your question then?
Member 11159188 12-May-15 23:45pm    
thanks for reply i have three different table the table stored in dataset i want to assign gridview thrid cell from 2nd table values this my source code

<columns>
<asp:TemplateField HeaderText="S.No">
<itemtemplate>
<div align="center">
<asp:Label ID="lblSerial" runat="server" Text="">
<asp:Label ID="lblWorkforceID" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "State") %>' >
</div>

<HeaderStyle Width="50px" />

<asp:TemplateField HeaderText="State">
<itemtemplate>
<asp:Label ID="lblFirstname1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "State") %>' />

<itemstyle horizontalalign="center">

<asp:TemplateField HeaderText="Fitter">
<itemtemplate>
<asp:Label ID="lblFitter" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TotalEmployee") %>' />

<itemstyle horizontalalign="Left">

<asp:TemplateField HeaderText="Welder">
<itemtemplate>
<asp:Label ID="lblAttendanceDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TotalEmployee") %>' />

<itemstyle horizontalalign="Left">

<asp:TemplateField HeaderText="Marker">
<itemtemplate>
<asp:Label ID="lblIntime" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TotalEmployee") %>' />

<itemstyle horizontalalign="Left">

<asp:TemplateField HeaderText="Grinder">
<itemtemplate>
<asp:Label ID="lblGrinder" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TotalEmployee") %>' />

<itemstyle horizontalalign="Left">

<asp:TemplateField HeaderText="Rigger">
<itemtemplate>
<asp:Label ID="lblRigger" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TotalEmployee") %>' />

<itemstyle horizontalalign="Left">

<asp:TemplateField HeaderText="Gas Cutter">
<itemtemplate>
<asp:Label ID="lblCutter" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "TotalEmployee") %>' />

The error means that either there are now rows and therefore .Rows[0] is null or There are not that many cells in your row.

Note, the cells will be zero based so perhaps Cells[10] does not exist.

However, this is a very easy thing for you to fix on your own if you just debug the code. Put a breakpoint on the code and you can see exactly what the problem is and how to fix it.

Pay attention to which line of code actually causes the error and then you'll know.
 
Share this answer
 
v2
The grid cells index starts from zero, so probably if your table contains 10 columns, then your grid columns should start from cells[0] to cells[9].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900