Sorted datagrid on a form and display selected row on a dialog-HELP

  • 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
 
Try PropertyManager.
private void Current_Changed(object sender, EventArgs e)
{
BindingManagerBase bm = (BindingManagerBase) sender;
/* Check the type of the Current object. If it is not a
DataRowView, exit the method. */
if(bm.Current.GetType() != typeof(DataRowView)) return;

// Otherwise, print the value of the column named "CustName".
DataRowView drv = (DataRowView) bm.Current;
Console.Write("CurrentChanged): ");
Console.Write(drv["CustName"]);
Console.WriteLine();
}

--
=====================================================
* ÃλÃÈçÕæ£¡ ¡¡¡¡*
* ÎÒºÜÕæ³Ï£¬µ«ÎÒÉú»îÔÚ»ÑÑÔÖ®ÖС£ ¡¡¡¡*
* ¡¡¡¡*
* ÎÒÅׯúÁËËùÓеÄÓÇÉËÓëÒÉÂÇ£¬È¥×·ÖðÄÇÎ޼ҵij±Ë®£¬ *
* ÖÕÓÚÕÒµ½ÁËÒ»¸ö°®ÎÒµÄÅ®ÈË£¬´Ó´ËʧȥÁË×ÔÓÉ£¡ ¡¡ *
*[csharp] °ßÖñ ÌÀ½¨¾ü ¡¡ ¡¡*
=====================================================
kll 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