I have a WIndows form in a C# application with a datagrid view called dgvCustomers. I now need to turn this into a lookup window so it will return the Customer number. There are only two fields on this form, CUSTNMBR and CUSTNAME. I am thinking of giving them two options to return a value. One would be with a button with text Select and the other would be a double click event. I assume the code would be the same between these two methods. The query behind this dgv is:
SELECT CUSTNMBR,CUSTNAME FROM CUSTOMERS ORDER BY CUSTNMBR
This query is used to populate a data table(Customers) and then the datagridview(dgvCustomers) is populated using this data table.
What is the proper method to identify the cell to which the user is currently pointing and returning the customer number?
I got this to work using these three methods. First I call the lookup window by clicking a button.
private void btnHistBatch_Click(object sender, EventArgs e)
{
using (frmLookupBatchIDs LookupBatchIDsForm = new frmLookupBatchIDs(batchNumber))
{
if (LookupBatchIDsForm.ShowDialog(this) == DialogResult.OK)
{
LookupBatchIDsForm.Show();
LookupBatchIDsForm.Activate();
LookupBatchIDsForm.Focus();
batchNumber = LookupBatchIDsForm.CellValue;
}
}
txtBatchID.Text = batchNumber;
}
-----------------------------------------------------------------------------------------
I then populate a datagridview
----------------------------------------------------------------------------------------
public frmLookupBatchIDs(string bachNumber)
{
InitializeComponent();
DataTable batchIDs = new DataTable();
int BatchRecords = DataAccess.GetListofSOPBatches(Dynamics.Globals.IntercompanyId, ref batchIDs);
if (BatchRecords == 0)
{
MessageBox.Show("No invoice batches found");
}
dgvBatchIDs.Columns[0].Width = 250;
dgvBatchIDs.Columns[0].HeaderText = "Batch IDs";
}
--------------------------------------------------------------------------------------
I then use method to select the value.
My question now is how does a value actually come back? I am using CellValue to return the value.
Is it simply
CurrentRow = dgvBatchIDs.CurrentCell.RowIndex;
dgvBatchIDs.CurrentCell = dgvBatchIDs[columnToReturn, CurrentRow];
CellValue = dgvBatchIDs.CurrentCell.Value.ToString();
CurrentRow gets the current highlighted row
dgvBatchIDs.CurrentCell gets the value in the current cell
CellValue is then assigned that value?
I think I am over thinking this. I am still trying to get used C#.
private void btnHistBatch_Click(object sender, EventArgs e)
{
using (frmLookupBatchIDs LookupBatchIDsForm = new frmLookupBatchIDs(batchNum
{
if (LookupBatchIDsForm.ShowDi
{
LookupBatchIDsForm.Show();
LookupBatchIDsForm.Activat
LookupBatchIDsForm.Focus()
batchNumber = LookupBatchIDsForm.CellVal
}
}
txtBatchID.Text = batchNumber;
}
--------------------------
I then populate a datagridview
--------------------------
public frmLookupBatchIDs(string bachNumber)
{
InitializeComponent();
DataTable batchIDs = new DataTable();
int BatchRecords = DataAccess.GetListofSOPBat
if (BatchRecords == 0)
{
MessageBox.Show("No invoice batches found");
}
dgvBatchIDs.ReadOnly = true;
dgvBatchIDs.RowHeadersVisi
dgvBatchIDs.DataSource = batchIDs;
dgvBatchIDs.Columns[0].Wid
dgvBatchIDs.Columns[0].Hea
}
--------------------------
I then use method to select the value.
--------------------------
private void btnSelect_Click(object sender, EventArgs e)
{
int CurrentRow;
int columnToReturn = 0;
CurrentRow = dgvBatchIDs.CurrentCell.Ro
dgvBatchIDs.CurrentCell = dgvBatchIDs[columnToReturn
CellValue = dgvBatchIDs.CurrentCell.Va
/* Retrieve Customer number */
custValue = dgvBatchIDs[columnToReturn
DialogResult = DialogResult.OK;
return;
}
--------------------------
My question now is how does a value actually come back? I am using CellValue to return the value.
Is it simply
CurrentRow = dgvBatchIDs.CurrentCell.Ro
dgvBatchIDs.CurrentCell = dgvBatchIDs[columnToReturn
CellValue = dgvBatchIDs.CurrentCell.Va
CurrentRow gets the current highlighted row
dgvBatchIDs.CurrentCell gets the value in the current cell
CellValue is then assigned that value?
I think I am over thinking this. I am still trying to get used C#.