design
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="502px"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onselectedindexchanged="GridView1_SelectedIndexChanged"
style="text-align: center" DataKeyNames="u_id">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%# Bind("u_name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("u_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
<asp:RadioButtonList ID="rdbGender" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtGender" runat="server" Text='<%# Bind("u_gender") %>' Visible="false"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("u_gender") %>'></asp:Label>
<asp:RadioButtonList ID="rdbGender" runat="server" RepeatDirection="Horizontal" Visible="false">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Class">
<EditItemTemplate>
<asp:DropDownList ID="ddlClass" runat="server" Height="16px" Width="88px"
onselectedindexchanged="ddlClass_SelectedIndexChanged">
</asp:DropDownList>
<asp:TextBox ID="txtClass" runat="server" Text='<%# Bind("c_name") %>' Visible="false"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("c_name") %>'></asp:Label>
<asp:DropDownList ID="ddlClass" runat="server" Height="16px" Width="88px" Visible="false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<%--
<asp:BoundField DataField="u_name" HeaderText="Name" />
<asp:BoundField DataField="u_gender" HeaderText="Gender" />
<asp:BoundField DataField="c_name" HeaderText="Class" />
<asp:CommandField ShowEditButton="True" />--%>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
Code
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
FillGrid();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtG = (TextBox)e.Row.FindControl("txtGender");
RadioButtonList rdb = (RadioButtonList)e.Row.FindControl("rdbGender");
if (txtG != null)
{
if (txtG.Text == "Male")
{
rdb.Items[0].Selected = true;
}
else
{
rdb.Items[1].Selected = true;
}
}
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlClass");
TextBox txtC = (TextBox)e.Row.FindControl("txtClass");
string str = "select * from tbl_class";
SqlDataAdapter adt = new SqlDataAdapter(str, con);
DataTable dt = new DataTable();
adt.Fill(dt);
ddl.DataSource = dt;
ddl.DataTextField = "c_name";
ddl.DataValueField = "c_id";
ddl.DataBind();
if (txtC != null)
{
ddl.SelectedValue = ddl.Items.FindByText(txtC.Text).Value;
}
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
FillGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
TextBox txtn = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
RadioButtonList rdbg = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rdbGender");
DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlClass");
string strt = ddl.SelectedValue;
TextBox txt= (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");
string str = "update tbl_user set u_name='" + txtn.Text + "',u_gender='" + rdbg.SelectedItem + "',u_c_id=" +Convert.ToInt32(strt) + "where u_id=" + id + "";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
FillGrid();
}