【问题标题】:editing one column of data grid view编辑一列数据网格视图
【发布时间】:2013-02-18 11:58:28
【问题描述】:

我知道如何在数据网格视图中编辑整行,我想知道如何仅编辑一列以保持其余数据相同。我的当前代码如下所示,我想知道如何仅编辑一列并将该数据更新到数据表中。我只想编辑“ab”列。

HTML 代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True"
              OnRowEditing="Gridview1_rowediting"  
              OnRowUpdating="Gridview_RowUpdating"
             OnRowCancelingEdit="Gridview_EditCanceling" >
</asp:GridView>

后面的C#代码

DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
    add();
    }
}

private void add()
{

    dt.Columns.Add("ab", typeof(string));
    dt.Columns.Add("ac", typeof(string));
    dt.Columns.Add("av", typeof(string));
    dt.Columns.Add("ax", typeof(string));
    DataRow row = dt.NewRow();
    row["ac"] = "sndasbfb";
    row["av"] = "sndasbfb";
    row["av"] = "sndasbfb";
    row["ax"] = "sndasbfb";
    dt.Rows.Add(row);

    GridView1.DataSource = dt;
    //ProjectsGridView.DataSource = dt;
    //ReleasesGridView.DataSource = dt;
    GridView1.DataBind();
    //  ProjectsGridView.DataBind();
    // ReleasesGridView.DataBind();
}

protected void Gridview1_rowediting(object sender, GridViewEditEventArgs e)
{
    //GridView1.EditIndex = e.NewEditIndex;
}

protected void Gridview_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GridView1.Rows[e.RowIndex];
}

protected void Gridview_EditCanceling(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    add();
}

【问题讨论】:

  • 当您声明要显示 GridView 中的列时,您可以使用属性“ReadOnly”。
  • 是的,但不能更改

标签: c# gridview datatable


【解决方案1】:

您可以在设计视图中或通过如下代码将列 ReadOnly 设置为 True:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        add();
    }

    //If you are using template field

    ((TemplateField)gvGridView.Columns[index]).EditItemTemplate = null;

    //If you are using boundfield

    ((BoundField)gvGridView.Columns[index]).ReadOnly = true;
}

或者您可以在设计视图中删除该列的编辑模板,将该列转换为模板。

查看此链接了解更多信息:

Gridview disable edit on 1 column asp.net

【讨论】:

    【解决方案2】:

    我在我的一个应用程序中尝试了此代码。

    foreach (DataGridViewRow c in this.DataGrid.Rows)
                        c.Cells[0].Value = "Something";
    

    这里的“0”是ColumnIndex。现在您可以根据需要设置条件。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多