索引超出范围 网格视图行

本文关键字:视图 网格 范围 索引 | 更新日期: 2023-09-27 18:37:02

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

第 252 行:按钮 _singleClickButton = (kButton)e.Row.Cells[0]。控件[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>

索引超出范围 网格视图行

代码中存在问题

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例外,因为行数可能多于单元格。使用定义的单元格编号获取特定单元格。

如果要使用 Button ,请在TemplateField中使用它,如下所示

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

使用如下所示FindControl很容易找到此Button

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