gpt4 book ai didi

c# - 索引超出范围 GridViewRow

转载 作者:行者123 更新时间:2023-11-30 22:01:16 29 4
gpt4 key购买 nike

代码按预期跳过了 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>

最佳答案

你的代码有问题

Button _singleClickButtonNew = (Button)e.Row.Cells[e.Row.RowIndex].Controls[0];
  1. 您正在将 ButtonField 转换为 Button
  2. 您正在访问基于 RowIndex (e.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
}

关于c# - 索引超出范围 GridViewRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27854201/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com