Mohammad Abbas Mohammad Abbas - 9 months ago 52 Question

Need to fill Gridview checkbox attribute from a cell data or database data

enter image description here

Hi guys,

I am not a developer but I always try my best to manage my page coding by myself before bothering anyone and by checking many examples and apply them to my web application, but this time I really surrendered and had to ask.

I have a Gridview where I will be using to update one field which is CHECKBOX.
The gridview has a checkbox control where it's checked attribute need to be True or False based on the Database value.
Values are 1 and 2 only

I wrote the below code to catch the cell label and change the checkbox attribute but it didn't work

Protected Sub Refill_checkbox(ByVal sender As Object, ByVal e As System.EventArgs)

Dim i As Integer

For i = 0 To dg.Rows.Count - 1
Dim Test As String = CType(dg.Rows(i).Cells(3).FindControl("Att_Type"), Label).Text

If CType(dg.Rows(i).Cells(4).FindControl("Att_Type"), Label).Text = 1 Then

CType(dg.Rows(i).Cells(6).FindControl("CheckBox_Attendance"), CheckBox).Checked = True
CType(dg.Rows(i).Cells(6).FindControl("CheckBox_Attendance"), CheckBox).Checked = False
End If

End Sub

Unfortunately it even did not write the Test Variable to find if it catches the label from the gridview or not.

My Gridview is as following:

<asp:GridView ID="dg" runat="server" BorderColor="#CCCCCC" BorderStyle="None" AutoGenerateColumns="False"
BorderWidth="1px" CellPadding="4"
EnableModelValidation="True" ForeColor="Black" GridLines="Horizontal"
Width="99%" AllowPaging="True" PageSize="500" DataSourceID="SqlDataSource_BCS" >
<asp:TemplateField HeaderText="#">
<%#Container.DataItemIndex + 1 %>

<asp:BoundField DataField="Att_ID" HeaderText="Att_ID"
SortExpression="Att_ID" />

<asp:BoundField DataField="Emp_FullName" HeaderText="Emp_FullName"
SortExpression="Emp_FullName" />

<asp:BoundField DataField="Att_Desc" HeaderText="Att_Desc"
SortExpression="Att_Desc" />

<asp:BoundField DataField="Att_Type" HeaderText="Att_Type"
SortExpression="Att_Type" />

<asp:BoundField DataField="Att_Date" HeaderText="Att_Date"
SortExpression="Att_Date" />

<asp:templatefield HeaderText="Attendance" >

<itemtemplate >

<asp:CheckBox ID="CheckBox_Attendance" runat="server" />

<itemstyle horizontalalign="left" />



<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="White" Font-Bold="True" BorderWidth="0px"/>
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BorderStyle="None" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />

I have no idea what is the correct way to do this.

Thanks in advance

Answer Source

You can bind your checkbox directly to the field like so...

<asp:CheckBox ID="CheckBox_Attendance" runat="server" Checked='<%# Eval("Att_type").ToString() = "1" %>'  />

Otherwise, handle it directly on your gridview rowdatabound event:

Put the following on your gridview:

<asp:GridView ... OnRowDatabound="dg_RowDataBound">

Change your TemplateField to look like this:

<asp:TemplateField HeaderText="Attendance">
        <asp:HiddenField id="hfType" Value='<%# Eval("Att_Type") %>' />
        <asp:CheckBox ID="CheckBox_Attendance" runat="server" />
    <ItemStyle HorizontalAlign="left" />

In your code behind

Protected Sub dg_RowDataBound(sender As Object, e As GridViewRowEventArgs)
      Dim cbAttendance As Checkbox = e.Row.FindControl("CheckBox_Attendance")
      Dim hfType as Hiddenfield = e.Row.FindControl("hfType")

      If hfType.Value = "1" Then
         cbAttendance.Checked = True

End Sub