0

I know how I can edit an entire row in the data grid view, I would like to know how to edit only one column keeping rest of the data the same. The current code that I have is given below and I would like to know how I would be able to just edit one column and update that data to the data table. I would like to edit only column "ab".

HTML CODE

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

C# code behind

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();
}
Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
user2082903
  • 3
  • 1
  • 6

2 Answers2

2

You can set columns ReadOnly to True in Design View or by code like this:

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;
}

Or you can remove the edit template for that column in design view converting the column to template.

Check this link for more info:

Gridview disable edit on 1 column asp.net

Community
  • 1
  • 1
RedDevil79
  • 445
  • 5
  • 13
0

I tried this code in one of my applications.

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

here "0" is the ColumnIndex. Now you could put conditions according to your needs.

DhavalR
  • 1,409
  • 3
  • 29
  • 57
  • I have tried this and it doesnt let me edit the code through the browser – user2082903 Feb 18 '13 at 12:28
  • try this links: http://www.codeproject.com/Articles/33339/Edit-Cells-in-Gridview-in-ASP-NET and http://www.codeproject.com/Articles/18136/Edit-Individual-GridView-Cells-in-ASP-NET – DhavalR Feb 18 '13 at 12:33