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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display cell value from datagrid on delete

Status
Not open for further replies.

kim20

Programmer
Nov 3, 2002
9
GB
Hi

i am creating a windows app.
I have a delete button which works ok as below.
On the delete i would like to display a value from the cell as string s but i am aware the string s line will not work because item is not part of Eventargs e.
how can i display the value from columnname("ProviderName") for the currentcell selected?

many thanks
chris

Code:
	private void button1_Click(object sender, System.EventArgs e)
		{
			try
			{
				string s = e.Item.Cells[0].Text; 
				DialogResult dr=MessageBox.Show("Are you sure you want to delete this row ? " +s, 
					"Confirm deletion",  MessageBoxButtons.YesNo, MessageBoxIcon.Question);
				if (dr ==DialogResult.Yes) 
				{

					DataTable tbl=new DataTable("ProvidersDetail");
					tbl=this.dsProviders.Tables[0];
					//				int i=dgProviders.iRowIndex;
					int i=dgProviders.CurrentRowIndex;
					tbl.Rows[i].Delete(); //delete the row
					this.daProviders.Update(tbl); //update the table
					dgProviders.Refresh();

					//				daProviders.Delete(dsProviders, tableName);
					MessageBox.Show("Record Deleted successfully","Delete Record Confirmed",
						MessageBoxButtons.OK,MessageBoxIcon.Information);
					//				dgProviders.Refresh();
				}		
			}
			catch (Exception E)
			{
				MessageBox.Show(E.ToString());
			}
		}
 
if Item isnt part of the EventArgs, then what is? Im pretty sure that tehre will be an index, indicating which row in the datagrid was clicked?

Failing that, can you look at the DataGrid.SelectedIndex property, and use that as the index in your dataset?

Though if you do this, and your datagrid is sortable, you might want to check that when the rows are sorted if the id's still match those in your datasource (Im not sure what happens there, Ive never really looked)

K
 
thanks

yeah i get system.eventargs does not contain a definition for item.

I can retrieve the row number by using the HitTestInfo method, but its the actual cell value of "ProviderName" i want. this ties in with my other problem of wanting to double-click a cell and launching a new form carrying over the cell value.

selectedindex is a property of a datagrid on a windows app ?

thanks
 
Ive only got .net2 here, but have you tried somehting like

dataGridView.SelectedRows[0].Index to get the index?

(or to guess in 1.1 dataGrid.SelectedItemIndex ?)

I can look this evening at a copy of 1.1 if u get no answers before then?

K
 
thanks K

I have made some progress. I can get the double-click working now.
i have the code below but it is not taking the ID value across to Form2, value is always 1.

this is the line i am trying to put the value as a public variable.
Code:
viewEditForm.form1TextBoxValue = (int) currentRowView["ID"];



Code:
private void GridClick()
		{
Form2 viewEditForm;
CurrencyManager dataTableCurrencyManager;
DataRowView  currentRowView;
DialogResult result;

if (_selectedRow > -1 && _selectedCol < 4)
			{

dataTableCurrencyManager = (CurrencyManager)this.BindingContext[dsProviders,"ProvidersDetail"];

currentRowView = (DataRowView) dataTableCurrencyManager.Current;

viewEditForm = new Form2();
				viewEditForm.form1TextBoxValue = (int) currentRowView["ID"];

result = viewEditForm.ShowDialog();

viewEditForm.Dispose();
viewEditForm = null;

if (result == DialogResult.OK) btnReset.PerformClick(); 
}
}

thanks
 
sorted it............it was because my datagrid was bound to a dataview but the currency manager was bound to the datatable.
working a treat now...............as my currency manager is linked to the dataview.

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top