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
Suthish NairPosted Dec 15, 2010, 12:47 PM
you can also try usign on GridView1_RowDataBound event
venkyPosted Dec 15, 2010, 12:31 PM
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 NairPosted Dec 15, 2010, 12:04 PM
Can you post the ConvertToBool method. Also, check the view source of page and find what value get binding on Checked=''?
venkyPosted Dec 15, 2010, 5:02 AM
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
CrishPosted Dec 15, 2010, 4:01 AM
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