Hi itsme,
Refer below sample.
HTML
<asp:CheckBox ID="Cashpay" Text="Cash" runat="server" CssClass="bg-info" />
<asp:CheckBox ID="Cardpay" Text="Card" runat="server" CssClass="bg-info" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Payment Mode">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Payment Mode">
<ItemTemplate>
<asp:Label ID="lblpay" runat="server" Text='<%#Eval("payment_mode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Payment Mode">
<ItemTemplate>
<asp:Button Text="Edit" runat="server" OnClick="btnEdit_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT Id, payment_mode FROM UpdatePaymentMode", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void btnEdit_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
GridViewRow grow = btn.NamingContainer as GridViewRow;
string value = (grow.FindControl("lblpay") as Label).Text;
if (value == "Cash")
{
Cashpay.Checked = true;
Cardpay.Checked = false;
}
if (value == "Card,Cash")
{
Cashpay.Checked = true;
Cardpay.Checked = true;
}
if (value == "Cash,Card")
{
Cashpay.Checked = true;
Cardpay.Checked = true;
}
if (value == "Card")
{
Cardpay.Checked = true;
Cashpay.Checked = false;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT Id, payment_mode FROM UpdatePaymentMode", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = TryCast(sender, Button)
Dim grow As GridViewRow = TryCast(btn.NamingContainer, GridViewRow)
Dim value As String = (TryCast(grow.FindControl("lblpay"), Label)).Text
If value = "Cash" Then
Cashpay.Checked = True
Cardpay.Checked = False
End If
If value = "Card,Cash" Then
Cashpay.Checked = True
Cardpay.Checked = True
End If
If value = "Cash,Card" Then
Cashpay.Checked = True
Cardpay.Checked = True
End If
If value = "Card" Then
Cardpay.Checked = True
Cashpay.Checked = False
End If
End Sub
Screenshot