i have gridview in my project and database records are displayed in that but if i want to edit the row then wht code should i write??
pls help me for this.

Recommended Answers

All 4 Replies

protected void Button1_Click(object sender, EventArgs e)
    { 
       connectionString = ("Data Source=INTRANETSERVER1;User ID=sa;Initial Catalog=HrData");
       SqlConnection myConnection = new SqlConnection(connectionString);
       string str = "Select srno,caseno,status,partyname,amount from courtcase_new where branchname ='"+ DropDownList1.Text +"' and finyear = '"+ DropDownList2.Text +"' and casecategory= '"+ DropDownList3.Text +"'";
       //SqlDataAdapter ad = new SqlDataAdapter(str, myConnection);
       SqlCommand cmd = new SqlCommand(str, myConnection);
       //ad.Fill(ds);
       cmd.Connection.Open();
       PeopleGridView.DataSource = cmd.ExecuteReader();
       PeopleGridView.DataBind();
       if (PeopleGridView.Rows.Count > 0)
       {
           PeopleGridView.Rows[0].Cells[0].Width = 50;
           PeopleGridView.Rows[0].Cells[1].Width = 50;
           PeopleGridView.Rows[0].Cells[2].Width = 100;
           PeopleGridView.Rows[0].Cells[3].Width = 500;
           PeopleGridView.Rows[0].Cells[4].Width = 50;
       }

      
    }
      
    protected void PeopleGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.PeopleGridView, "Select$" + e.Row.RowIndex);
        }
    }

i have written following code but i m getting error at this line
if (e.Row.RowType == DataControlRowType.DataRow)

System.Web.UI.WebControls.GridViewEditEventArgs' does not contain a definition for 'Row'

Row isn't member of GridViewEditEventArgs. Use e.NewEditIndex property

if (GridView1.Rows[e.NewEditIndex].RowType==DataControlRowType.DataRow)
    {
      GridView1.Rows[e.NewEditIndex].Attributes["key_here"] = "value";    
    }

thanks ..
now i want to show 5 column i have dine that but in that 4 columns shold not get updates or user should dont have authority to update tht.

hi,

This can be done using TemplateFields

<asp:GridView ID="" runat="" autogeneratecolumns=".....>
<columns>
  <!--Add 5 TemplateFields for your 5 columns -->
  <!--lets say only the first column is updatable -->
   <asp:TemplateField>
       <itemTemplate>
          <asp:Label ID="lblSalary" runat="server" Text='<%#Eval('Dbcolname')%>'>         
       </itemTemplate>

       <EditItemTemplate>
           <asp:TextBox ID="txtSalary" runat="server" Text='<%#Eval('Dbcolname')%>'> 
       <EditItemTemplate>
  </asp:TemplateField>

   <asp:TemplateField>
      <itemTemplate>......</itemTemplate>
   </asp:TemplateField>

   <asp:TemplateField>
     <itemTemplate>......</itemTemplate>
   </asp:TemplateField>

   <asp:TemplateField>
    <itemTemplate>.... </itemTemplate>
  </asp:TemplateField>

   <asp:TemplateField>
     <itemTemplate>......</itemTemplate>
   </asp:TemplateField>
</columns>

When user clicks on the Edit button, the fields that have EditItemTemplate defined for it will show up that control that has been declared in EdititemTemplate. In our case textbox txtSalary for column 1, allowing user to enter values.

Other rows dont have the EdititemTemplate defined and hence will always show up the label declare in <ItemTemplate>.

And you know how to handle the 'UPDATE/DELETE'. Let me know if you need more info

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.