HELP-Sorted Datagrids and modal dialogs

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have been on newsgroup for week. I have seen anything that will help me. Basically, I have a form with a datagrid that can be sorted or unsorted (it is up to the user). Then, I display the data of the selected row in textboxes on dialog that execute by the user press a button. The following code works if I do not sort the datagrid before I press the button to bring up the dialog. It is when I sort it the datagrid I have the issue

private void Frm_CustomerAU_Load(object sender, System.EventArgs e

Frm_Customers frmCustomers= (Frm_Customers) this.Owner;

CurrencyManager currencyMgr = (CurrencyManager)this.BindingContext[frmCustomers.tableInfoDG.DataSource, "Table"]

currencyMgr.Position = frmCustomers.tableInfoDG.CurrentRowIndex

DataRow currentRow = ((DataRowView)currencyMgr.Current).Row

txtBxCompanyName.Text= currentRow["CompanyName"].ToString()

I have also tried using the following code to bind the table to the textbox and I get the same result
txtBxCompanyName.DataBindings.Add("Text", frmCustomers.tableInfoDG.DataSource, "Table.CompanyName")

I believe it has something to do what I am setting CurrencyManager position to ( currencyMgr.Position = frmCustomers.tableInfoDG.CurrentRowIndex;). I know I need it to be the position of the underlying table in order to get the selected position in the datagrid. I just do not know how to get the position. By the way, this is C

Any help would be greatly appreciated
 
There are a few way to go about this, but one easy one is this. When you
are loading your datatable. walk through it in its original state and add
the ColumnValue (like your primary key or any field that uniquely identifies
the record) to the hash table as its key with the original position as the
value. So if you have a column lastname, and under the original sort, Ryan
was the first record, Add Ryan as the Key of the hashtable and 0 as the
value.

Now, whenever you click on the row, look for it's lastname value and do a
find on the hashtable. It's extremely fast even with large amounts of data.
Now you'll know that Ryan = 0 regardless of the fact that if you sorted on
last name, Ryan's position may be toward the end of the sort. This Index is
what you can use for the datatable
help said:
I have been on newsgroup for week. I have seen anything that will help
me. Basically, I have a form with a datagrid that can be sorted or unsorted
(it is up to the user). Then, I display the data of the selected row in
textboxes on dialog that execute by the user press a button. The following
code works if I do not sort the datagrid before I press the button to bring
up the dialog. It is when I sort it the datagrid I have the issue.
private void Frm_CustomerAU_Load(object sender, System.EventArgs e)
{
Frm_Customers frmCustomers= (Frm_Customers) this.Owner;

CurrencyManager currencyMgr = (CurrencyManager)this.BindingContext[frmCustomers.tableInfoDG.DataSource,
"Table"];

currencyMgr.Position = frmCustomers.tableInfoDG.CurrentRowIndex;

DataRow currentRow = ((DataRowView)currencyMgr.Current).Row;

txtBxCompanyName.Text= currentRow["CompanyName"].ToString();
}
I have also tried using the following code to bind the table to the
textbox and I get the same results
txtBxCompanyName.DataBindings.Add("Text",
frmCustomers.tableInfoDG.DataSource, "Table.CompanyName");
I believe it has something to do what I am setting CurrencyManager
position to ( currencyMgr.Position =
frmCustomers.tableInfoDG.CurrentRowIndex;). I know I need it to be the
position of the underlying table in order to get the selected position in
the datagrid. I just do not know how to get the position. By the way, this
is C#
 
Back
Top