Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete row in datagrid

Status
Not open for further replies.

flnMichael

Programmer
Nov 13, 2003
96
US
Hey,
How do I get the row number if I want to use the delete command for a datagrid in C#? I'm trying to delete a row, but am unsure of how to get the associated row.

Thanks
Mike
 
nevermind, I got it....but now I have another problem. I have a delete command that doesn't delete...anybody have a clue why this doesn't work???

Code:
private void KPC_Data_Grid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
 KPC_Data_Grid.EditItemIndex = e.Item.ItemIndex;

 string SelectCmd = "SELECT * FROM KPC where OPID ='" + myOPID + "'";

 SqlConnection myConnSelect = new SqlConnection(strConnect);
 myConnSelect.Open();
 myCommand = new SqlCommand(SelectCmd, myConnSelect);

 adapter = new SqlDataAdapter(SelectCmd, myConnSelect);

 adapter.Fill(kpC_Data_Set1, "KPC");
	
 IntMyKPCID = kpC_Data_Set1.KPC[KPC_Data_Grid.EditItemIndex].KPCID;

 adapter = new SqlDataAdapter(SelectCmd, myConnSelect);

 string DeleteCmd = "DELETE from KPC where KPCID = " + IntMyKPCID + "";

 SqlConnection myConnSelect2 = new SqlConnection(strConnect);
 myConnSelect2.Open();
 myCommand = new SqlCommand(DeleteCmd, myConnSelect2);

 KPC_Data_Grid.DataSource = kpC_Data_Set1.KPC.DefaultView;
 KPC_Data_Grid.EditItemIndex=-1;
 KPC_Data_Grid.DataBind();

 adapter.Fill(kpC_Data_Set1);

 RefillFullGrid();

}

and the RefillFullGrid():
Code:
string SelectCmd = "SELECT * FROM KPC where OPID ='" + myOPID + "'";

SqlConnection myConnSelect = new SqlConnection(strConnect);
myConnSelect.Open();
myCommand = new SqlCommand(SelectCmd, myConnSelect);

myCommand.ExecuteNonQuery();
myCommand.Connection.Close();

myCommand.Connection.Close();

adapter = new SqlDataAdapter(SelectCmd, myConnSelect);

adapter.Fill(kpC_Data_Set1, "KPC");

It gives me no error, but when I display the screen again, I still have the row I wanted deleted. I also check the database to see if I may have just re-posted the data from memory, but it's also still there...

Thanks in advance
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top