Here, I have created Sample code that will help you.
HTML
<div>
<asp:GridView ID="gvattendence" runat="server" AutoGenerateColumns="false" OnRowEditing="gvattendence_RowEditing"
OnRowDataBound="gvattendence_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="StudentId">
<ItemTemplate>
<asp:Label ID="lblStudentId" Text='<%#Eval("StudentId") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StudentName">
<ItemTemplate>
<asp:Label ID="lblStudentName" Text='<%#Eval("StudentName") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtStudentName" Text='<%#Eval("StudentName") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStudentStatus" Text='<%#Eval("Status") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblStudentStatus" Text='<%#Eval("Status") %>' Visible="false" runat="server" />
<asp:CheckBox ID="chkpresent" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetStudentsDetails();
}
}
private void GetStudentsDetails()
{
DataTable dt = new DataTable();
dt.Columns.Add("StudentId", typeof(int));
dt.Columns.Add("StudentName", typeof(string));
dt.Columns.Add("Status", typeof(string));
dt.Rows.Add(1, "John", "Present");
dt.Rows.Add(2, "Peter", "Absent");
gvattendence.DataSource = dt;
gvattendence.DataBind();
}
protected void gvattendence_RowEditing(object sender, GridViewEditEventArgs e)
{
gvattendence.EditIndex = e.NewEditIndex;
this.GetStudentsDetails();
}
protected void gvattendence_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvattendence.EditIndex == e.Row.RowIndex)
{
CheckBox chekpresent = (e.Row.FindControl("chkpresent") as CheckBox);
string status = (e.Row.FindControl("lblStudentStatus") as Label).Text;
if (status == "Present")
{
chekpresent.Checked = true;
}
else if (status == "Absent")
{
chekpresent.Checked = false;
}
}
}
VB
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
Me.GetStudentsDetails()
End If
End Sub
Private Sub GetStudentsDetails()
Dim dt As New DataTable()
dt.Columns.Add("StudentId", GetType(Integer))
dt.Columns.Add("StudentName", GetType(String))
dt.Columns.Add("Status", GetType(String))
dt.Rows.Add(1, "John", "Present")
dt.Rows.Add(2, "Peter", "Absent")
gvattendence.DataSource = dt
gvattendence.DataBind()
End Sub
Protected Sub gvattendence_RowEditing(sender As Object, e As GridViewEditEventArgs)
gvattendence.EditIndex = e.NewEditIndex
Me.GetStudentsDetails()
End Sub
Protected Sub gvattendence_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow AndAlso gvattendence.EditIndex = e.Row.RowIndex Then
Dim chekpresent As CheckBox = TryCast(e.Row.FindControl("chkpresent"), CheckBox)
Dim status As String = TryCast(e.Row.FindControl("lblStudentStatus"), Label).Text
If status = "Present" Then
chekpresent.Checked = True
ElseIf status = "Absent" Then
chekpresent.Checked = False
End If
End If
End Sub
Screenshot
I hope this code help you.