Hello there
I am new to programming
i am trying to update my checkbox column in a gridview which i cont
I can able to get the data 1 or 0 in database and able to update the checkbox as checked or unchecked
my code is
please help in this bug i am scratching my head for last 3 days
thanks in advance
venky

CrishRank #1,487 · 1.1K reputationPosted Dec 14, 2010, 11:01 PM EST
You can refer following code for Gridview checkbox.
private void btnSubmit_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dr in myDataGridView.Rows)
{
if(dr.Cells[0].Value != null) //Cells[0] Because in cell 0th cell we have added checkbox
{
MessageBox.Show("Rows " +dr.Index + " selected");
}
}
}
But if you want to get more detail about this code and you want to know how to use datagridview with check box than just go for this below link.
http://www.dotnetspark.com/kb/151-add-checkbox-inside-datagridview-windows.aspx
venky5 reputationPosted Dec 15, 2010, 12:02 AM EST
But what i am trying to do is i just kept Enable editing in the gridview properties it automatically generates update botton
what i need to was if i click on edit button(automatically generated) and select the chck/unselect the checkbox and clieck on update button the database has to be updated to 0/1.
its an web application
Thanks in advance
Suthish NairRank #92 · 22.8K reputationPosted Dec 15, 2010, 7:04 AM EST
Can you post the ConvertToBool method. Also, check the view source of page and find what value get binding on Checked=''?
venky5 reputationPosted Dec 15, 2010, 7:31 AM EST
public bool ConvertToBool(object state)
{
return (state.ToString() == "1");
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string Book_Id = GridView1.DataKeys[e.RowIndex].Value.ToString();
//string Book_Id = GridView1.Rows[e.RowIndex].Cells[0].ToString();
string Available = "1";
SqlConnection dbCon = new SqlConnection(cns);
dbCon.Open();
var dbCmd = new System.Data.SqlClient.SqlCommand();
dbCmd.Connection = dbCon;
dbCmd.CommandType = CommandType.Text;
dbCmd.CommandText = "UPDATE [Book_sales] set [Available]=" + Available + "WHERE [Book_Id] = " + Book_Id;
dbCmd.ExecuteNonQuery();
}
Thanks in advance
Suthish NairRank #92 · 22.8K reputationPosted Dec 15, 2010, 7:47 AM EST
you can also try usign on GridView1_RowDataBound event