【问题标题】:GridView update from Button Click event crashes从按钮单击事件崩溃的 GridView 更新
【发布时间】:2015-06-26 18:28:44
【问题描述】:

在 ButtonClick 事件上填充网格视图后,我正面临 Array 异常。当它从页面加载触发时,相同的源工作得很好。在 stackoverflow 上找不到类似的帖子。请指教。谢谢。

代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Attributes["width"] = "30px";
    e.Row.Cells[1].Attributes["width"] = "40px";   -> CRASHES AT INDEX 1
    e.Row.Cells[2].Attributes["width"] = "40px";
    e.Row.Cells[3].Attributes["width"] = "50px";
    e.Row.Cells[4].Attributes["width"] = "50px";
    e.Row.Cells[5].Attributes["width"] = "100px";
    e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Left;
}

<asp:GridView ID="GridView1" runat="server" CellPadding="4"
    ForeColor="Black" GridLines="Horizontal" Width="100%" ShowHeader="true" 
    EmptyDataText="No Records found.!" 
    RowStyle-Wrap="true" AllowPaging="true" PageSize="5"
    OnPageIndexChanging="gridView_PageIndexChanging"
    OnRowDataBound="GridView1_RowDataBound"         
    style="table-layout:fixed; word-wrap:break-word; margin-left: 0px;" >

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您应该检查一下它是什么类型的行。我正在检查 DataRow,但您可能只想对 Header 执行此操作。

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Attributes["width"] = "30px";
            e.Row.Cells[1].Attributes["width"] = "40px";   -> CRASHES AT INDEX 1
            e.Row.Cells[2].Attributes["width"] = "40px";
            e.Row.Cells[3].Attributes["width"] = "50px";
            e.Row.Cells[4].Attributes["width"] = "50px";
            e.Row.Cells[5].Attributes["width"] = "100px";
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Left;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      相关资源
      最近更新 更多