gpt4 book ai didi

c# - 在 GridView 上添加行得到 "Specified argument was out of the range of valid values"

转载 作者:太空宇宙 更新时间:2023-11-03 12:51:31 24 4
gpt4 key购买 nike

我试图根据某些条件在 GridVIew 上添加一些行,但出现“指定参数超出有效值范围”错误。这是我的 GridView :

<asp:GridView ID="gvConcept" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="false" OnRowDataBound="gvConcept_RowDataBound" OnRowCreated="gvConcept_RowCreated" >
<Columns>
<asp:BoundField DataField="Concept" HeaderText=" Concept" />
<asp:BoundField DataField="Client" HeaderText=" Client" />
<asp:BoundField DataField="YTD" HeaderText=" YTD" ItemStyle-HorizontalAlign="Right" />
</Columns>
</asp:GridView>

这是我的代码隐藏:

string ParAnt = string.Empty;
string partner = string.Empty;
protected void gvConcept_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ParAnt = DataBinder.Eval(e.Row.DataItem, "Partner").ToString();
}
}

public void AddNewRow(object sender, GridViewRowEventArgs e, int rowIndex)
{
GridView GridView1 = (GridView)sender;
GridViewRow NewTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
NewTotalRow.Font.Bold = true;
NewTotalRow.ForeColor = System.Drawing.Color.Black;
NewTotalRow.BackColor = System.Drawing.Color.LightGray;
TableCell HeaderCell = new TableCell();
HeaderCell.Height = 10;
HeaderCell.HorizontalAlign = HorizontalAlign.Left;
HeaderCell.ColumnSpan = 4;
HeaderCell.Text = partner;
NewTotalRow.Cells.Add(HeaderCell);
GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + rowIndex, NewTotalRow);
}

protected void gvConcept_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
partner = DataBinder.Eval(e.Row.DataItem, "Partner").ToString();
if (ParAnt != partner)
{
AddNewRow(sender, e, e.Row.RowIndex+1);
ParAnt = DataBinder.Eval(e.Row.DataItem, "Partner").ToString();
}
}
}

第一行已正确添加,但无法添加下一行(在我的案例 2 中)。任何输入任何人?谢谢。

最佳答案

我相信您应该使用 1 作为最后一个参数调用 AddNewRow 函数,而不是 e.RowRowIndex + 1。在第一次调用时,这会将 1 作为值 (0 + 1) 传递,但在第二次调用时,它将传递 2 (1 + 1) 和 InsertAt AddAt 将为索引 3 调用:e.Row.RowIndex + 2 = 3。

关于c# - 在 GridView 上添加行得到 "Specified argument was out of the range of valid values",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35362205/

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