wilko100
asked on
Deleting from a DataGridView control
Im extremly new to C# and i have a datagridview polpulated already but would like to know how i can delete a row in SQL using the datagridcontrol. I can delete a row from the datagridview but i want to apply that to SQL server db table too. Can anyone help?
I can specify the connection, DataAdaptor etc but im stuck after that. I would need to create A SQL DELETE command and update at the end but thats all i know
I can specify the connection, DataAdaptor etc but im stuck after that. I would need to create A SQL DELETE command and update at the end but thats all i know
private void btnDelete_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection();
conn = new SqlConnection(@"Data Source=<SQLInstance>;Initial Catalog=<Database>;Integrated Security=True");
da = new SqlDataAdapter();
ds = new DataSet();
datagridview1.Rows.RemoveAt(datagridview1.CurrentRow.Index); //applys delete to the gid but i want to delete the record in SQL too
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well in SQL Delete you are going to delete based on a unique value of the row in question. Usually this is the primary key column.
I cant tell you exactly without building one myself, but I suspect it is something like this:
datagridview1.Rows(datagri dview1.Cur rentRow.In dex).cells (index of your value).value;
Funny thing I wasnt aware a gridview had a property for "currentrow". It's not listed on MSDN as having that property, but you said you had it working already using that "removeat" code so Im going with it.
Dday
I cant tell you exactly without building one myself, but I suspect it is something like this:
datagridview1.Rows(datagri
Funny thing I wasnt aware a gridview had a property for "currentrow". It's not listed on MSDN as having that property, but you said you had it working already using that "removeat" code so Im going with it.
Dday
ASKER
Yep the currentrow works a treat and really easy when its disconnected, im clueelss when i want to delete from within the backend! I used...(See code below) which removed the row selected, i just dont know what the syntax is for deleting the selected row
Would you guys say this is the order to delete?:
1) Create connection
2) Create DELETE Command (DELETE Counter, FirstName, LastName etc FROM [Table] WHERE Counter = "+Selected Row on Grid+");
3) Create DataAdaptor
4) Create Dataset
5) Update
Would you guys say this is the order to delete?:
1) Create connection
2) Create DELETE Command (DELETE Counter, FirstName, LastName etc FROM [Table] WHERE Counter = "+Selected Row on Grid+");
3) Create DataAdaptor
4) Create Dataset
5) Update
private void btnDelete_Click(object sender, EventArgs e)
{
[GridControlName].Rows.RemoveAt([GridControlName].CurrentRow.Index);
}
I don't often use dataadapters as I use the disconnected model more often. I'm 99% sure thought that all you need do is assign the sqldataadapter.deletecomma
A short example:
da.DeleteCommand = "DELETE FROM tbl WHERE ColumnId =" + [GetValueFromDataGridView HERE]
da.Delete
Now fire through your original binding process, or since you've already removed the row you may not need to.
A more full scale example can be found at this link:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.deletecommand(VS.71).aspx