【问题标题】:Disable a row in gridview based on column name根据列名禁用gridview中的一行
【发布时间】:2017-05-09 12:47:56
【问题描述】:

我正在使用下面的代码来禁用 gridview 中列名为 test 的行。除第一行外,一切正常。颜色不会应用于第一行。我哪里出错了?我还想根据列名隐藏一列。

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (Hidden4.Value == "Data Present")
    {
        foreach (GridViewRow item in GdvTestData.Rows)
        {
            int a = GetColumnIndexByName(item, "test");
            int b = GetColumnIndexByName(item, "id");

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string test = e.Row.Cells[a].Text;

                foreach (TableCell cell in e.Row.Cells)
                {
                    if (test == "Y")
                    {
                        cell.BackColor = Color.Gray;
                        e.Row.Attributes.Add("onmouseover", "alert('This data is for testing');");
                    }
                }
                e.Row.Cells[b].Visible = false;
            }
        }
    }
}

【问题讨论】:

  • 当您已经可以访问RowDataBound 事件中的当前行 (e.Row) 时,为什么要循环 GridViewRows(首先对每一行执行)?
  • 如何重写 .如果我不使用循环,则无法使用 GetColumnIndexByName。
  • 循环错误。这意味着您将为每一行都这样做。

标签: c# asp.net gridview rowdatabound


【解决方案1】:

也许您混淆了数据/业务逻辑和 UI。

RowDataBound 事件每行有效,因此您无需循环 gridview 行。

您正在处理单元格内容,但您有数据绑定到这些内容。所以:

ASPX

<asp:GridView runat="server" ID="gv" ...
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>              
                        <asp:Label runat="server" Text='<%# Eval("Test") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                ...

代码背后

private void GrdBind()
{
    // populate datatable
    gv.DataSource = myDataTable;
    gv.Databind();
}

还有数据绑定事件:

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (Hidden4.Value == "Data Present")
        { 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               DataRow row = ((DataRowView)e.Row.DataItem).Row;
               string test = row.Field<String>("Test");

               if (test== "Y")
               {
                  e.Row.Cells[0].BackColor = Color.Gray; // set your index
                  e.Row.Attributes.Add("onmouseover", "alert('This data is for testing');");
               }
            }
       }
    }

【讨论】:

  • 什么是 item.Test 和 foo 的用途。我也在使用 int a = GetColumnIndexByName(item, "test");参考列名test获取索引。
  • 我编辑了我之前的帖子。你要绑定什么样的数据到 GridView?
  • 从sql查询其数据表
  • 我又编辑了一遍。尝试检查。这次我没有测试。
  • 它不仅仅是添加背景色。我还想根据列名隐藏一列。在 gridview 加载时,我需要隐藏 id 列。
【解决方案2】:

你可以这样做。虽然我不明白你禁用一行是什么意思。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the dataitem back to a datarowview 
        DataRowView row = e.Row.DataItem as DataRowView;

        //find the column value in the row, not the cell text
        string test = row["columnName"].ToString();

        //check the value and if so apply backcolor
        if (test == "Y")
        {
            e.Row.BackColor = Color.Red;

            //or hide the row (no need to do this in Databind or OnPreRender)
            e.Row.Visible = false;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    相关资源
    最近更新 更多