I was working on a web part basically it was an attendance web part that contains Name and Present column only and display as GridView. Name contains the full name of employee and present contains Yes/No value for that employee. I took the present column as checkbox column that displays values using checked or unchecked. Now my present data comes in integer form as 1 and 0 and checkbox takes true or false so while binding
integer value to checkbox it was throwing “Specified cast is invalid” error. After applying so many alternate and googling i couldn’t get the right way. So i developed my own way to do this…
I was binding the gridview with a data table that was querying to a custom list. Now i just add another column named as “Attendance” that contains boolean value depending upon “Present” column using expressions. So here is the code.
After getting a data table it contains two columns “Name” and “Present” with data now add a new column using code behind.
DataColumn dcAttendance = new DataColumn();
dcAttendance.DataType = System.Type.GetType(“System.Boolean”);
dcAttendance.ColumnName = “Attendance”; // New data column that will hold true and false.
dcAttendance.Expression = “Present”; // Existing data column contains values 0 and 1.
grdAttendance.DataSource = dt;
Now in page inline code write:
<asp:GridView ID=”grdAttendance” runat=”server” AutoGenerateColumns=”false”>
<asp:BoundField DataField=”Name” HeaderText=”Name” />
<asp:CheckBox ID=”chkAttendance” runat=”server” Checked='<%# Bind(“Attendance”) %>’ />