代码按预期跳过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>
答案 0 :(得分:3)
您的代码中存在问题
Button _singleClickButtonNew = (Button)e.Row.Cells[e.Row.RowIndex].Controls[0];
ButtonField
投射到Button
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>
使用Button
可以轻松找到FindControl
,如下所示
Button btn = (e.Row.FindControl("btn") as Button);
if(btn != null)
{
//add button related code
}