0

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

3
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by ishamputra
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.

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.

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.

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...

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
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..

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;
            }
        }
    }
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

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.