Hello everyone :)

i want to be able to just edit and update one field of my grid view is this possible?

as im using Visual Web developer 2008 express

when a user selects edit to a row, it allows the user to edit the whole row ... but this means they can change a lot of information, which i dont want them to for example

My grid view shows the following row to a moderator which authorises student marks given by a tutor

Student id, Firstname, lastname, Total Marks, Grade, Authorise, Reason

now authorise in this case is a boolean -

so the user in this case should only be able to change one field which is authorise ....

can anyone help please :)

Dani AI

Generated

Short answer: yes — allow editing of only the boolean "Authorise" cell by keeping the grid in view mode and exposing a single editable control (a CheckBox or an "Authorize" button) that updates just that column. 's template-field idea is good for full edit-mode control; an alternative that avoids showing textboxes for every column is to bind a CheckBox in a TemplateField and update the boolean directly on change.

Example (GridView markup + minimal code-behind):

<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="False" DataKeyNames="StudentID">
  <Columns>
    <asp:BoundField DataField="StudentID" HeaderText="Student ID" ReadOnly="True" />
    <asp:BoundField DataField="Firstname" HeaderText="Firstname" ReadOnly="True" />
    <asp:BoundField DataField="Lastname" HeaderText="Lastname" ReadOnly="True" />
    <asp:TemplateField HeaderText="Authorise">
      <ItemTemplate>
        <asp:CheckBox ID="chkAuthorise" runat="server"
          Checked='<%# Eval("Authorise") %>'
          AutoPostBack="true" OnCheckedChanged="chkAuthorise_CheckedChanged" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>
protected void chkAuthorise_CheckedChanged(object sender, EventArgs e)
{
  var chk = (CheckBox)sender;
  var row = (GridViewRow)chk.NamingContainer;
  int id = (int)gvStudents.DataKeys[row.RowIndex].Value;
  bool auth = chk.Checked;

  using(var conn = new SqlConnection(connString))
  using(var cmd = new SqlCommand("UPDATE Students SET Authorise = @Auth WHERE StudentID = @ID", conn))
  {
    cmd.Parameters.Add("@Auth", SqlDbType.Bit).Value = auth;
    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
    conn.Open();
    cmd.ExecuteNonQuery();
  }

  // Rebind or refresh the row as needed
  gvStudents.DataBind();
}

Notes and cautions: set DataKeyNames so the primary key is available; always use parameterized queries to avoid SQL injection; handle exceptions and rebind logic so the CheckBox state is stable after postback. For a declarative route, a SqlDataSource with an UpdateCommand that updates only the Authorise column can also be used. 's DataGrid example shows row-level update handling; the inline CheckBox approach is simpler when only one boolean needs to change.

Recommended Answers

All 3 Replies

It is very simple.
What you need to do first is,convert each of the columns of your row in to a template field.

do it as follows
1) Click on the GridView's Top-right corner,i.e smart menu
2)Click on Edit Columns
3) Select each of your column and Click on Convert this into a template field

These steps will convert all fields into template field
It means now you have four different view for your single columns
1--> Item Template -- that actually shows data in simple mode while you see the page
Basically it consist of a label control
2-->Alternating template
3-->EditItem Template -- that is shown when you click on Edit button
Onlclicking on edit button,The item template is set visible false and Edit Item template is shown
Edit Item template contains textboxes


So know click on grid view's smart menu once again
You will see Edit Templates
Click on It

1 ) Select a field that you want not to display while editing the row
2 )Go to it's Edit item template and simply delete everything given inside that template,mostly it contains a single textbox for editing
3 ) Put a label control inside Edit Item template
4 ) Bind that newly created label with your current field,by clicking on smart menu.


hope it helps :)

Thanks man really appreciate all your help bro :D

Try this. Hope it helps

public void edited(object sender, DataGridCommandEventArgs e)
    {
        dg.Columns[0].HeaderText = "Edit";
        dg.EditItemIndex = (int)e.Item.ItemIndex;
        BindGrid();
    }

    public void cancelled(object sender, DataGridCommandEventArgs e)
    {
        dg.Columns[0].HeaderText = "Cancel";
        dg.EditItemIndex = -1;
        BindGrid();
    }

    public void updated(object sender, DataGridCommandEventArgs e)
    {
        sqlcon.Open();
        dg.Columns[0].HeaderText = "Update";
        string myupdate = "update Student set StudentID=@StudentID, StudentName=@StudentName, Age=@Age, Course=@Course, Semester=@Sem, Marks=@Marks, Grade=@Grade where StudentID=@StudentID";
        SqlCommand cmd = new SqlCommand(myupdate, sqlcon);

        cmd.Parameters.Add(new SqlParameter("@StudentID", SqlDbType.Int));
        cmd.Parameters.Add(new SqlParameter("@StudentName", SqlDbType.VarChar, 25));
        cmd.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int));
        cmd.Parameters.Add(new SqlParameter("@Course", SqlDbType.VarChar, 20));
        cmd.Parameters.Add(new SqlParameter("@Sem", SqlDbType.Int));
        cmd.Parameters.Add(new SqlParameter("@Marks", SqlDbType.Int));
        cmd.Parameters.Add(new SqlParameter("@Grade", SqlDbType.Char, 13));

        cmd.Parameters["@StudentID"].Value = dg.DataKeys[(int)e.Item.ItemIndex]; //used for primary key        
        cmd.Parameters["@StudentName"].Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
        cmd.Parameters["@Age"].Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
        cmd.Parameters["@Course"].Value = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
        cmd.Parameters["@Sem"].Value = ((TextBox)e.Item.Cells[5].Controls[0]).Text;
        cmd.Parameters["@Marks"].Value = ((TextBox)e.Item.Cells[6].Controls[0]).Text;
        cmd.Parameters["@Grade"].Value = ((TextBox)e.Item.Cells[7].Controls[0]).Text;

        try
        {
            cmd.ExecuteNonQuery();
            Response.Write("<font color=blue> Record(s) Updated! </font>");
            dg.EditItemIndex = -1;
        }
        catch (SqlException ex)
        {
            if (ex.Number == 157)
            {
                Response.Write("<font color=red> Error! record already exists with the same primary key </font>");
            }
            else
            {
                Response.Write("<font color=red> Updating failed! </font>");
            }
        }
        sqlcon.Close();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.