Select (Check) duplicate record on CheckBox Check in ASP.Net GridView using C# and VB.Net

Last Reply 4 months ago By dharmendr

Posted 4 months ago

i have gridview as follows

Selectdata  Slipno  Product
              10      NPK
              10      NPK
              12      UREE
              15      ENGRA

 

<asp:gridview ID="GridView1" runat="server" 
    <Columns>
        <asp:TemplateField  HeaderText="Select Data">
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:gridview>

for example, if i select the slip no value 10 using check box, similarly slip no 10 in the second row is automatically checked in the gridview.

My code as follows

        GridViewRow selectedRow = (sender as CheckBox).NamingContainer as GridViewRow;
        string selectedName = selectedRow.Cells[1].Text;
        DataTable dt = ViewState["Data"] as DataTable;

        foreach (GridViewRow gvrow in GridView1.Rows)
        {
            ViewState["Data"] = dt;
            CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
            if (chk != null & chk.Checked)
            {
                int duplicateCount = dt.Select("Slipno='" + selectedName + "'").Length;
                if (duplicateCount > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["Slipno"].ToString() == selectedName)
                        {
                            dt.Rows[i]["Select Data"] = true;
                        }
                    }
                }
            }
        }

When i run the above code shows error as follows

Column 'Select Data' does not belong to table Table1.

how to solve the above error.

You are viewing reply posted by: dharmendr 4 months ago.
Posted 4 months ago

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