Check (Select) CheckBox in GridView from database based on condition using C# in ASP.Net

lingers
 
on Oct 07, 2021 04:32 AM
629 Views

I have this page that has gridview with ckeckboxes, it insert and updates values in single quote e.g 'die33'.On page load the check box is checked or unchecked if a cell value is present in the database.

While i wrote the code the value in the database is in single quote e.g 'die33', so it reads perfectly well into the check box.But the actual database has double quote around the values e.g "die33",so the values are not read into the check box even if the cell value is present in the database ,because the double quoted is not programmed in my code. What i want is for my code to insert and update in double quote so that my code will read from the actual database of the project

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Results 1 - 5 of 6
Andrea
 
on Oct 07, 2021 04:32 AM
Hi @lingers,
Please try the following

How to check a CheckBox in GridView based on Database value in ASP.Net

It might help you.

Cheers Andrea.
dharmendr
 
on Oct 07, 2021 07:24 AM
lingers says:
List<string> CylinderNo = acceptedCylinder.Replace("[", "").Replace("]", "").Replace("'", "").Split(',').ToList();

Replace with below.

List<string> CylinderNo = acceptedCylinder.Replace("[", "").Replace("]", "").Replace("\"", "").Split(',').ToList();

 

lingers
 
on Oct 07, 2021 04:28 PM
on Oct 07, 2021 04:31 PM

it did not work, it can't read value in double quote into the checkbox. when it is inserting ,it insert value in single quote as well.

arjunv
 
on Oct 08, 2021 07:14 AM
dharmendr says:
List<string> CylinderNo = acceptedCylinder.Replace("[", "").Replace("]", "").Replace("\"", "").Split(',').ToList();

Ignore this changes.

Change the RowDataBound event code with below.

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string id = Label27.Text;
        string[] cylinders = GetAcceptedCylinder(id).Replace("[", "").Replace("]", "").Split(',');
        CheckBox chkSelect = e.Row.FindControl("chkSelect") as CheckBox;
        if (cylinders.Contains("\"" + e.Row.Cells[2].Text + "\""))
        {
            chkSelect.Checked = true;
        }
        else
        {
            chkSelect.Checked = false;
        }
    }
}

 

lingers
 
on Oct 08, 2021 04:55 PM

it doesn't read value into the check box and it insert values in single quote in the database e.g ['23a','23b']

Results 1 - 5 of 6