954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Check Gridview Data against SQL records

help! i have a gridview with checkbox item template, and on click, those rows that are checked will be inserted into the database. When i close and run my application again, i want those rows previously inserted to remain checked. Means that i have to retrieve data from the database and check against my gridview data....Please advise with C# codes. thkz alot

ishamputra
Newbie Poster
12 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

If database column bound to the database is boolean type, then the checkbox in the template column will automatically checked.

See my code snippet in this link: http://www.daniweb.com/code/snippet1248.html

In the above code snippet, i have bound a checkbox template column to a boolean field in a DataTable which is created temprorily(not retrieved from database). Change that code slightly to address you requirement.

Ramesh S
Posting Pro
583 posts since Jun 2009
Reputation Points: 165
Solved Threads: 113
 

If database column bound to the database is boolean type, then the checkbox in the template column will automatically checked.

See my code snippet in this link: http://www.daniweb.com/code/snippet1248.html

In the above code snippet, i have bound a checkbox template column to a boolean field in a DataTable which is created temprorily(not retrieved from database). Change that code slightly to address you requirement.

sorry, i dont quite understand. is there a more simplified way?
on page load, the gridview checkbox item will check those rows that already exist in the database.

ishamputra
Newbie Poster
12 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

wat i have done is fill my records to a dataset, but i cant find a way to loop and compare the boundfield of the gridview to the dataset to find the same text, and if there is a same text, checkbox item will be checked...

ishamputra
Newbie Poster
12 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This code checks checkboxs in a gridview on page load. i believe you can use this. I am sorry this code is in vb but i hope it still helps maybe someone can rewrite it out in c#. please tell me if it's what you need

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For Each row As GridViewRow In GridView1.Rows
            Dim cb As CheckBox = row.FindControl("Checkbox1")
            If cb IsNot Nothing Then
                Dim productID As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)
                If productID = 1 Then ' Here you can have any condition
                    cb.Checked = True  'checks the box on page load
                End If
            End If
        Next
    End Sub
End Class
steve11
Newbie Poster
7 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

This code checks checkboxs in a gridview on page load. i believe you can use this. I am sorry this code is in vb but i hope it still helps maybe someone can rewrite it out in c#. please tell me if it's what you need

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For Each row As GridViewRow In GridView1.Rows
            Dim cb As CheckBox = row.FindControl("Checkbox1")
            If cb IsNot Nothing Then
                Dim productID As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)
                If productID = 1 Then ' Here you can have any condition
                    cb.Checked = True  'checks the box on page load
                End If
            End If
        Next
    End Sub
End Class

thanks, its similar but not likely the codes i wanted...since i need to compare against dataset..

ishamputra
Newbie Poster
12 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

i did something like this...pls advise...

public void CheckCoordinator()
    {
        DataSet ds = stfDAL.RetrieveCoordinator();
        for (int i = 0; i < grdStaffManager.Rows.Count; i++)
        {
            if ()// statement to compare the 1st columns of the gridview against the dataset.
            {
                CheckBox cb = (CheckBox)grdStaffManager.Rows[i].Cells[4].FindControl("chkCoordinator");
                cb.Checked = true;
            }
        }
    }
ishamputra
Newbie Poster
12 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

DataSet ds = stfDAL.RetrieveCoordinator();

foreach (DataRow dr in ds.Tables[0].Rows)
{
for (int i = 0; i < grdStaffManager.Rows.Count; i++)
{
if (dr["staffID"].ToString() == grdStaffManager.Rows[i].Cells[0].Text)
{
CheckBox cb = (CheckBox)grdStaffManager.Rows[i].Cells[4].FindControl("chkCoordinator");
cb.Checked = true;
}
}
}

i figured it out myself...thanks guys

ishamputra
Newbie Poster
12 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You