Hi narasiman,
This error means column with name Select Data which does not exists in your table.
I have modified the code.
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Select Data">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" Checked='<%# Convert.ToBoolean(Eval("Select Data")) %>'
AutoPostBack="true" OnCheckedChanged="CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Slipno" HeaderText="Slipno" />
<asp:BoundField DataField="Product" HeaderText="Product" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = GetData();
ViewState["Data"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
private static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Slipno", typeof(int)), new DataColumn("Product", typeof(string)) });
dt.Rows.Add(10, "NPK");
dt.Rows.Add(10, "NPK");
dt.Rows.Add(12, "UREE");
dt.Rows.Add(25, "ENGRA");
DataColumn selectData = new DataColumn("Select Data", typeof(bool));
selectData.DefaultValue = false;
dt.Columns.Add(selectData);
return dt;
}
protected void CheckedChanged(object sender, EventArgs e)
{
GridViewRow selectedRow = (sender as CheckBox).NamingContainer as GridViewRow;
string selectedName = selectedRow.Cells[1].Text;
DataTable dt = ViewState["Data"] as DataTable;
CheckBox chk = (sender as CheckBox);
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["Slipno"].ToString() == selectedName)
{
if (chk.Checked)
{
dt.Rows[i]["Select Data"] = true;
}
else
{
dt.Rows[i]["Select Data"] = false;
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = GetData()
ViewState("Data") = dt
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Private Shared Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Slipno", GetType(Integer)), New DataColumn("Product", GetType(String))})
dt.Rows.Add(10, "NPK")
dt.Rows.Add(10, "NPK")
dt.Rows.Add(12, "UREE")
dt.Rows.Add(25, "ENGRA")
Dim selectData As DataColumn = New DataColumn("Select Data", GetType(Boolean))
selectData.DefaultValue = False
dt.Columns.Add(selectData)
Return dt
End Function
Protected Sub CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim selectedRow As GridViewRow = TryCast((TryCast(sender, CheckBox)).NamingContainer, GridViewRow)
Dim selectedName As String = selectedRow.Cells(1).Text
Dim dt As DataTable = TryCast(ViewState("Data"), DataTable)
Dim chk As CheckBox = (TryCast(sender, CheckBox))
For i As Integer = 0 To dt.Rows.Count - 1
If dt.Rows(i)("Slipno").ToString() = selectedName Then
If chk.Checked Then
dt.Rows(i)("Select Data") = True
Else
dt.Rows(i)("Select Data") = False
End If
End If
Next
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Screenshot