Dave: If I understand you correctly, you can use the
dataGrid.CurrentRowIndex or the DataGrid.CurrentCell. CurrentRowIndex will
give you the row (it's a single int value) and current cell will give you
both coordinates. So if you want to reference that row, you can use the
CurrentRowIndex of the datatable for instance (although this would require
the datatable to be in the same order as the grid was originally).
You can have a new window that has a DataRow as a property, I'd add an
overloaded constructor which includes a DataRow as a param and then you can
set it from there. ONce it's set, you, when the form loads you can bind a
grid to that row, or you can have x comboboxes and you can bind to them or
whatever. You'll when the form closes, those values will still be visible
b/c a DataRow is a reference type, so the changes to it will affect the
orignal row. It should happen automatically. If you want to manually do
it, then you will have access to the dataRow property you just set. From
there, you can grab the values and do as you see fit with them.
Someone eariler asked me how they could launch a new form with a listbox
from the double click and then return the value from that listbox and insert
it into the grid. I'm pasting the code below:
Ok, let's say Main if frmMain and your form with the listbox is frmList and
there's a listbox on it. Create a property on frmList called ListValue or
whatever you want. I'm writing this in C# but the same holds for vb.net
Here's the property and event handler for frmLIst:
public string ListItem
{
get{return listItem;}
set{listItem = value;}
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
if(listBox1.SelectedIndex > -1)
{
this.ListItem = listBox1.SelectedItem.ToString();
}
}
Use this button too to ensure that the user selects something... private
void button1_Click(object sender, System.EventArgs e)
{
if(listBox1.SelectedIndex > -1)
{
this.ListItem = listBox1.SelectedItem.ToString();
this.DialogResult = DialogResult.OK;
}
}
You may want to do something to make sure the user selects something.... or,
if not, in frmMain, make sure that dialogresult.Cancel isn't selected. Make
sure you set the AcceptButton and CancelButton properties of frmLIst
ok, now on frmMain, you have a grid and when the user clicks on it, you fire
form2, then use the results from frmLIst to set the value in the grid.
NOtice that I used grid column 2. This could be any column you wanted , I
just chose 2, IF you wanted 3, then use (i,3) for instance.
I commented the code and tested it, this will work if I understood you
correctly. The only thing you need to do is test for the dialogresult in
the first form in case the user clicks Cancel (if you have a cancel button)
or just closes the form..you don't want null reference exceptions or
anything like that. Also, note that I used a string but you could use
anytying just set the property accordingly.
private void dataGrid1_DoubleClick(object sender, System.EventArgs e)
{
int i = dataGrid1.CurrentRowIndex;
frmList listForm = new frmList();
listForm.ShowDialog();
//Ok, this is the value (s) that will correspond with the value
//the user selected in the listbox. You don't have to use a string,
//you could use a datarow, integer, whatever, to do this, you'd chagne
//the property in the frmList and then change the type here.
//Now you can set the value of whatever cell in the datagrid and
//set it to s
string s = listForm.ListItem;
//We'll set the currentcell in the datagrid to row 1, column2 but we
//could do this with anything. Before I called frmList, I'd check the
currecnt
//cell and check it's rowindex then, assuming the value I wanted to replace
was column2
//in the row I called the form from, I'd use dataGrid.Cu
dataGrid1[i, 2] = s;
}
If you have any questions, let me know. (CC me at this address so I'll get
it at work or home and can respond faster.)
Cheers,
Bill