Hi rakeshkuma,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvCompany" runat="server" AutoGenerateColumns="false" Width="30%"
OnRowDataBound="OnRowDataBound" CssClass="table table-bordered table-striped"
DataKeyNames="t_code">
<Columns>
<asp:TemplateField HeaderText="#" HeaderStyle-Width="20px" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label runat="server" ID="lblpono" Text=' <%# Container.DataItemIndex + 1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left">
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" AutoPostBack="true" ID="chkDel" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company Name" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Font-Bold="true">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("t_name") %>'></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("t_name") %>' CssClass="form-control" Visible="false" Width="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Font-Bold="true">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("t_add") %>'></asp:Label>
<asp:DropDownList ID="ddlCountries" runat="server" CssClass="form-control" Visible="false" Width="100%">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<hr />
<asp:Button ID="btnUpdate" runat="server" Text="UPDATE" CssClass="btn btn-primary btn-xs" OnClick="Update" Visible="false" />
<asp:Button ID="btnDelete" runat="server" Text="DELETE" CssClass="btn btn-primary btn-xs" OnClick="Delete" Visible="false" />
<asp:TextBox ID="txtrowindex" runat="server" Text='4' CssClass="form-control" placeholder="Type RowIndex" Width="200px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="CHECK" CssClass="btn btn-primary btn-xs" OnClick="Check" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
this.BindGrid();
}
private void BindGrid()
{
SqlCommand cmd = new SqlCommand("SELECT t_code, t_name, t_add FROM t_commast");
gvCompany.DataSource = this.ExecuteQuery(cmd, "SELECT");
gvCompany.DataBind();
}
private DataTable ExecuteQuery(SqlCommand cmd, string action)
{
string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
switch (action)
{
case "SELECT":
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
return null;
}
}
protected void Update(object sender, EventArgs e)
{
foreach (GridViewRow row in gvCompany.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[1].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{
SqlCommand cmd = new SqlCommand("UPDATE t_commast SET t_name = @t_name, t_add = @t_add WHERE t_code = @t_code");
cmd.Parameters.AddWithValue("@t_name", row.Cells[2].Controls.OfType<TextBox>().FirstOrDefault().Text);
cmd.Parameters.AddWithValue("@t_add", row.Cells[3].Controls.OfType<DropDownList>().FirstOrDefault().SelectedItem.Value);
cmd.Parameters.AddWithValue("@t_code", gvCompany.DataKeys[row.RowIndex].Value);
this.ExecuteQuery(cmd, "SELECT");
}
}
}
btnUpdate.Visible = false;
btnDelete.Visible = false;
this.BindGrid();
}
protected void Delete(object sender, EventArgs e)
{
foreach (GridViewRow row in gvCompany.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{
SqlCommand cmd = new SqlCommand("DELETE FROM t_commast WHERE t_code = @t_code");
cmd.Parameters.AddWithValue("@t_code", gvCompany.DataKeys[row.RowIndex].Value);
this.ExecuteQuery(cmd, "SELECT");
}
}
}
btnUpdate.Visible = false;
btnDelete.Visible = false;
this.BindGrid();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlCommand cmd = new SqlCommand("SELECT DISTINCT(t_add) FROM t_commast");
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
ddlCountries.DataSource = this.ExecuteQuery(cmd, "SELECT");
ddlCountries.DataTextField = "t_add";
ddlCountries.DataValueField = "t_add";
ddlCountries.DataBind();
string country = (e.Row.FindControl("lblCountry") as Label).Text;
ddlCountries.Items.FindByValue(country).Selected = true;
}
}
protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = (sender as CheckBox);
if (chk.ID == "chkAll")
{
foreach (GridViewRow row in gvCompany.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
row.Cells[1].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
}
}
CheckBox chkAll = (gvCompany.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = true;
foreach (GridViewRow row in gvCompany.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[1].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 2; i <= row.Cells.Count - 1; i++)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
if (isChecked && !isUpdateVisible)
isUpdateVisible = true;
if (!isChecked)
chkAll.Checked = false;
}
}
}
btnUpdate.Visible = isUpdateVisible;
btnDelete.Visible = isUpdateVisible;
}
protected void Check(object sender, EventArgs e)
{
int index = Convert.ToInt32(txtrowindex.Text);
if (gvCompany.Rows.Count > index)
{
GridViewRow row = gvCompany.Rows[index];
CheckBox c = (CheckBox)row.FindControl("chkDel");
if (c.Checked == false)
{
c.Checked = true;
OnCheckedChanged(c, e);
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim cmd As SqlCommand = New SqlCommand("SELECT t_code, t_name, t_add FROM t_commast")
gvCompany.DataSource = Me.ExecuteQuery(cmd, "SELECT")
gvCompany.DataBind()
End Sub
Private Function ExecuteQuery(ByVal cmd As SqlCommand, ByVal action As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
Select Case action
Case "SELECT"
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Select
Return Nothing
End Using
End Function
Protected Sub Update(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvCompany.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(1).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
If isChecked Then
Dim cmd As SqlCommand = New SqlCommand("UPDATE t_commast SET t_name = @t_name, t_add = @t_add WHERE t_code = @t_code")
cmd.Parameters.AddWithValue("@t_name", row.Cells(2).Controls.OfType(Of TextBox)().FirstOrDefault().Text)
cmd.Parameters.AddWithValue("@t_add", row.Cells(3).Controls.OfType(Of DropDownList)().FirstOrDefault().SelectedItem.Value)
cmd.Parameters.AddWithValue("@t_code", gvCompany.DataKeys(row.RowIndex).Value)
Me.ExecuteQuery(cmd, "SELECT")
End If
End If
Next
btnUpdate.Visible = False
btnDelete.Visible = False
Me.BindGrid()
End Sub
Protected Sub Delete(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvCompany.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(0).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
If isChecked Then
Dim cmd As SqlCommand = New SqlCommand("DELETE FROM t_commast WHERE t_code = @t_code")
cmd.Parameters.AddWithValue("@t_code", gvCompany.DataKeys(row.RowIndex).Value)
Me.ExecuteQuery(cmd, "SELECT")
End If
End If
Next
btnUpdate.Visible = False
btnDelete.Visible = False
Me.BindGrid()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cmd As SqlCommand = New SqlCommand("SELECT DISTINCT(t_add) FROM t_commast")
Dim ddlCountries As DropDownList = (TryCast(e.Row.FindControl("ddlCountries"), DropDownList))
ddlCountries.DataSource = Me.ExecuteQuery(cmd, "SELECT")
ddlCountries.DataTextField = "t_add"
ddlCountries.DataValueField = "t_add"
ddlCountries.DataBind()
Dim country As String = (TryCast(e.Row.FindControl("lblCountry"), Label)).Text
ddlCountries.Items.FindByValue(country).Selected = True
End If
End Sub
Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim isUpdateVisible As Boolean = False
Dim chk As CheckBox = (TryCast(sender, CheckBox))
If chk.ID = "chkAll" Then
For Each row As GridViewRow In gvCompany.Rows
If row.RowType = DataControlRowType.DataRow Then
row.Cells(1).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked = chk.Checked
End If
Next
End If
Dim chkAll As CheckBox = (TryCast(gvCompany.HeaderRow.FindControl("chkAll"), CheckBox))
chkAll.Checked = True
For Each row As GridViewRow In gvCompany.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim isChecked As Boolean = row.Cells(1).Controls.OfType(Of CheckBox)().FirstOrDefault().Checked
For i As Integer = 2 To row.Cells.Count - 1
row.Cells(i).Controls.OfType(Of Label)().FirstOrDefault().Visible = Not isChecked
If row.Cells(i).Controls.OfType(Of TextBox)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of TextBox)().FirstOrDefault().Visible = isChecked
End If
If row.Cells(i).Controls.OfType(Of DropDownList)().ToList().Count > 0 Then
row.Cells(i).Controls.OfType(Of DropDownList)().FirstOrDefault().Visible = isChecked
End If
If isChecked AndAlso Not isUpdateVisible Then
isUpdateVisible = True
End If
If Not isChecked Then
chkAll.Checked = False
End If
Next
End If
Next
btnUpdate.Visible = isUpdateVisible
btnDelete.Visible = isUpdateVisible
End Sub
Protected Sub Check(ByVal sender As Object, ByVal e As EventArgs)
Dim index As Integer = Convert.ToInt32(txtrowindex.Text)
If gvCompany.Rows.Count > index Then
Dim row As GridViewRow = gvCompany.Rows(index)
Dim c As CheckBox = CType(row.FindControl("chkDel"), CheckBox)
If c.Checked = False Then
c.Checked = True
OnCheckedChanged(c, e)
End If
End If
End Sub
Screenshot