tshoemake
tshoemake

Reputation: 1351

Index out of range GridViewRow

The code skips on Header as expected and enters when it becomes DataRow.. then it breaks immediately on the below error every time. Can someone please help?

Line 252: Button _singleClickButton = (kButton)e.Row.Cells[0].Controls[0];

 protected void Grd_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.VerticalAlign = VerticalAlign.Top;
            MonthData month = Data[e.Row.RowIndex];

            **Button _singleClickButtonNew = (Button)e.Row.Cells[e.Row.RowIndex].Controls[0];**
             string _jsSingleNew = ClientScript.GetPostBackClientHyperlink(_singleClickButtonNew, "");


                 foreach (DayData day in month.DayDatas)
                 {
                     int index = month.DayDatas.IndexOf(day) + 1;

                     SortProjectsByStartDate(day.Projects);
                     foreach (ProjectInfo project in day.Projects)
                     {
                         Button button = new Button();
                         if (day.ContainsProjectStart(project))
                         {
                             button.BackColor = Color.FromName(project.Color);
                             button.Click += btnProjectStart_Click;
                             button.CommandArgument = project.Id.ToString();
                         }
                         else if (day.ContainsProjectEnd(project))
                         {
                             button.Click += btnProjectStart_Click;
                             button.BackColor = Color.FromName(project.Color);
                             button.ToolTip = project.Name;
                         }
                         else
                         {
                             button.BackColor = Color.FromName(project.Color);
                             button.Click += btnProjectStart_Click;
                             button.CommandArgument = project.Id.ToString();
                             button.ToolTip = project.Name;
                         }

                         SortProjectsByStartDate(day.Projects);
                         button.Width = 60;
                         button.Height = 15;
                         button.ToolTip = project.Name;
                         //e.Row.Cells[index].Controls.Add(button);

                         e.Row.Cells[index].ToolTip = project.Name;

                         if (e.Row.RowIndex != -1)
                         {
                             e.Row.Cells[index].Attributes["onmouseover"] = "showContents('" + e.Row.Cells[1].Text + " " + (index - 1) + "')";
                             e.Row.Cells[index].Attributes["onmouseout"] = "hideContents()";
                             //e.Row.Attributes["onmouseover"] = "showContents('"  + (e.Row.RowIndex + 1) + "')";
                         }

                         // Add the column index as the event argument parameter  
                         string js = _jsSingleNew.Insert(_jsSingleNew.Length - 2, index.ToString());
                         // Add this javascript to the onclick Attribute of the cell  
                         e.Row.Cells[index].Attributes["onclick"] = js;
                         // Add a cursor style to the cells  
                         e.Row.Cells[index].Attributes["style"] += "cursor:pointer;cursor:hand;";
                     }

                 }
             }

MY grid:

 <asp:GridView ID="Grd" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False"  style="table-layout:fixed;" Width="1500px" Height="800px"
                        OnRowDataBound="Grd_RowDataBound"  OnRowCommand="Grd_RowCommand" 
                        CssClass="grid" BorderColor="Black" BorderStyle="Solid">
                        <Columns>
                            <asp:BoundField DataField="MonthName" HeaderText="Month"/>
                            <asp:BoundField HeaderText="1" />
                            <asp:BoundField HeaderText="2" />
                            <asp:BoundField HeaderText="3" />
                            <asp:BoundField HeaderText="4" />
                            <asp:BoundField HeaderText="5" />
                            <asp:BoundField HeaderText="6" />
                            <asp:BoundField HeaderText="7" />
                            <asp:BoundField HeaderText="8" />
                            <asp:BoundField HeaderText="9" />
                            <asp:BoundField HeaderText="10" />
                            <asp:BoundField HeaderText="11" />
                            <asp:BoundField HeaderText="12" />
                            <asp:BoundField HeaderText="13" />
                            <asp:BoundField HeaderText="14" />
                            <asp:BoundField HeaderText="15" />
                            <asp:BoundField HeaderText="16" />
                            <asp:BoundField HeaderText="17" />
                            <asp:BoundField HeaderText="18" />
                            <asp:BoundField HeaderText="19" />
                            <asp:BoundField HeaderText="20" />
                            <asp:BoundField HeaderText="21" />
                            <asp:BoundField HeaderText="22" />
                            <asp:BoundField HeaderText="23" />
                            <asp:BoundField HeaderText="24" />
                            <asp:BoundField HeaderText="25" />
                            <asp:BoundField HeaderText="26" />
                            <asp:BoundField HeaderText="27" />
                            <asp:BoundField HeaderText="28" />
                            <asp:BoundField HeaderText="29" />
                            <asp:BoundField HeaderText="30" />
                            <asp:BoundField HeaderText="31" />
                        </Columns>
                    </asp:GridView>

Upvotes: 2

Views: 553

Answers (1)

Bharadwaj
Bharadwaj

Reputation: 2553

There are issues in your code

Button _singleClickButtonNew = (Button)e.Row.Cells[e.Row.RowIndex].Controls[0];
  1. You are casting ButtonField to Button
  2. You are accessing cell based on RowIndex (e.Row.Cells[e.Row.RowIndex]). You only have few cells and this gives Index out of range exception as there may be more rows than cells. Use defined cell number to get a particular cell.

If you want to use Button, use it in TemplateField as below

<asp:TemplateField HeaderText="header">
    <ItemTemplate>
       <asp:Button id="btn" runat="server" Text="btn" OnClick="btn_Click"/>
    </ItemTemplate>
<asp:TemplateField>

This Button is easy to find using FindControl as below,

Button btn = (e.Row.FindControl("btn") as Button);
if(btn != null)
{
   //add button related code
}

Upvotes: 3

Related Questions