索引超出范围GridViewRow

时间:2015-01-09 05:11:07

标签: c# asp.net gridview

代码按预期跳过Header并在它变为DataRow时进入..然后每次都会在下面的错误中立即中断。有人可以帮忙吗?

第252行:按钮_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;";
                     }

                 }
             }

我的网格:

 <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>

1 个答案:

答案 0 :(得分:3)

您的代码中存在问题

Button _singleClickButtonNew = (Button)e.Row.Cells[e.Row.RowIndex].Controls[0];
  1. 您正在将ButtonField投射到Button
  2. 您正在访问基于RowIndexe.Row.Cells[e.Row.RowIndex])的单元格。您只有很少的单元格,这会导致Index out of range异常,因为行可能比单元格多。使用定义的单元格编号来获取特定单元格。
  3. 如果您想使用Button,请在TemplateField中使用,如下所示

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

    使用Button可以轻松找到FindControl,如下所示

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