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 :)

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.