【问题标题】:Manipulating GridView TemplateFields Depending on DataTable Values根据 DataTable 值操作 GridView 模板字段
【发布时间】:2015-05-29 00:00:03
【问题描述】:

背景

这是场景: 我有一个有五列的GridView

  1. asp:CommandField 设置为 readonly
  2. asp:BoundField 设置为 readonly
  3. 另一个asp:BoundField 设置为readonly
  4. 另一个asp:BoundField 设置为readonly
  5. asp:TemplateField 在此字段下带有以下内容。
    • EditItemTemplate
      • asp:TextBox

为简单起见,我们假设GridView 绑定到的DataTable 只有两行。

目标

我想要实现的是,一旦页面加载完毕并且数据表绑定到网格视图,我希望网格视图的第一行基于第一行单元格 5 的值处于编辑模式在数据表中。通过编辑模式,我的意思是要显示 TemplateField 中的 TextBox 以及要显示 CommandField 列中的更新按钮。
由于网格视图的第二行处于正常模式,基于第二行的值,数据表中的单元格 5,同时CommandField 列显示更新链接,然后使TextBox 在第二个可编辑网格视图的行。

单元格值的位置是不分青红皂白的。我知道如何指定查看哪个单元格来确定行的状态,我只是不知道如何设置行状态以进行编辑,以及数据源绑定上的 TextBox 可查看。

【问题讨论】:

    标签: c# asp.net gridview datatable


    【解决方案1】:

    您可以在 Grid 的 DataBound 事件或 RowDataBound 事件上执行:

    1) Grid_DataBound:

    protected void grid1_DataBound(object sender, EventArgs e)
        {
            int editIndex = -1;
            // find the row index which is editable
    
    
            grid1.EditIndex = editIndex;
            // Set EditIndex = -1 to set the row to normal mode
        }
    

    2) 行数据绑定

    protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                bool isEdit = false;
                // check for the condition to determine the state of the row
    
                if (isEdit)
                {
                    e.Row.RowState = DataControlRowState.Edit;
                }
            }
        }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多