Get the correct data row in an own DataGridColumn class

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

Hello,

i created an own DataGridColumn. In this class i get the corresponding data
with the GetColumnValueAtRow method. Now i will display all new lines in the
DataGrid in Red. So i inserted an additional field called 'New'.

My problem is that i cannot access the correct data row. I tried to access
the line with the following line of code:
NewFlag = (bool) ((DataRowView)
Source.Current).DataView.Table.Rows[RowNum].ItemArray[((DataRowView)
Source.Current).DataView.Table.Columns.IndexOf("New")];

But this works only, if the DataGrid is not sorted. In the other case the
row data are not identically with the row data from the GetColumnValueAtRow
method. So, if anybody knows, how i get the correct information, please
answer.

Thanks

Frank
 
You need to manually work with the CurrencyManager. Follow the link below
for an example that shows all about using CurrencyManager with DataGrids.
It was made for educational purposes such as this...

http://www.zrgwortz.com/datagrids.zip

(I am not the author of this code, nor am I responsible for hosting this
file. There were no viruses or malicious code at the download site last
time I downloaded it, but I can not be held responsible for the destination
of the link. I was given this link at the July 2003 meeting of the St Louis
CSharp.NET user group as part of the presentation.)

The important part is on form 'frmCurrencyManager1.cs', "updateInfo()
method...
============================================================================
==
private void updateInfo()
{
int index;
int totrows;
index = dgCustomers.CurrentRowIndex;
//show info based on DataGrid properties:
lblCurrentRowIndex1.Text = "CurrentRowIndex = " + index.ToString();
totrows = ds.Tables["Customers"].Rows.Count;
lblDataTableCount.Text = "DataRows = " + totrows.ToString();
lblCustomerID1.Text = "CustomerID = " +
ds.Tables["Customers"].Rows[index]["CustomerID"].ToString();

//show info based on CurrencyManager:
CurrencyManager cm;
cm = (CurrencyManager)dgCustomers.BindingContext[dgCustomers.DataSource,
dgCustomers.DataMember];
lblCurrentRowIndex2.Text = "CurrentRowIndex = " + index.ToString();
//get the # of rows currently displayed in the grid:
totrows = cm.Count;
lblCurrencyManagerCount.Text = "CurrencyManager Count = " +
totrows.ToString();
//get the DataRow referenced by the current row in the grid:
DataRow dr = ((DataRowView)cm.Current).Row; // THIS IS WHAT YOU WANT!!!
lblCustomerID2.Text = "CustomerID = " + dr["CustomerID"].ToString();
}
============================================================================
==

You can also find other useful winforms binding code at:

(George Shepherd's Windows Forms FAQ)
http://www.syncfusion.com/FAQ/WinForms/

Michael Lang, MCSD

Frank said:
Hello,

i created an own DataGridColumn. In this class i get the corresponding data
with the GetColumnValueAtRow method. Now i will display all new lines in the
DataGrid in Red. So i inserted an additional field called 'New'.

My problem is that i cannot access the correct data row. I tried to access
the line with the following line of code:
NewFlag = (bool) ((DataRowView)
Source.Current).DataView.Table.Rows[RowNum].ItemArray[((DataRowView)
Source.Current).DataView.Table.Columns.IndexOf("New")];

But this works only, if the DataGrid is not sorted. In the other case the
row data are not identically with the row data from the GetColumnValueAtRow
method. So, if anybody knows, how i get the correct information, please
answer.

Thanks

Frank
 
Hello Michael,

thanks for your answer. In the meantime i solved my problem. The correct
code for my problem is:

NewFlag = (bool) ((DataRowView)
Source.Current).DataView[RowNum].Row.ItemArray[((DataRowView)
Source.Current).DataView.Table.Columns.IndexOf("New")];

Thanks

Frank

Michael Lang said:
You need to manually work with the CurrencyManager. Follow the link below
for an example that shows all about using CurrencyManager with DataGrids.
It was made for educational purposes such as this...

http://www.zrgwortz.com/datagrids.zip

(I am not the author of this code, nor am I responsible for hosting this
file. There were no viruses or malicious code at the download site last
time I downloaded it, but I can not be held responsible for the destination
of the link. I was given this link at the July 2003 meeting of the St Louis
CSharp.NET user group as part of the presentation.)

The important part is on form 'frmCurrencyManager1.cs', "updateInfo()
method...
============================================================================
==
private void updateInfo()
{
int index;
int totrows;
index = dgCustomers.CurrentRowIndex;
//show info based on DataGrid properties:
lblCurrentRowIndex1.Text = "CurrentRowIndex = " + index.ToString();
totrows = ds.Tables["Customers"].Rows.Count;
lblDataTableCount.Text = "DataRows = " + totrows.ToString();
lblCustomerID1.Text = "CustomerID = " +
ds.Tables["Customers"].Rows[index]["CustomerID"].ToString();

//show info based on CurrencyManager:
CurrencyManager cm;
cm = (CurrencyManager)dgCustomers.BindingContext[dgCustomers.DataSource,
dgCustomers.DataMember];
lblCurrentRowIndex2.Text = "CurrentRowIndex = " + index.ToString();
//get the # of rows currently displayed in the grid:
totrows = cm.Count;
lblCurrencyManagerCount.Text = "CurrencyManager Count = " +
totrows.ToString();
//get the DataRow referenced by the current row in the grid:
DataRow dr = ((DataRowView)cm.Current).Row; // THIS IS WHAT YOU WANT!!!
lblCustomerID2.Text = "CustomerID = " + dr["CustomerID"].ToString();
}
============================================================================
==

You can also find other useful winforms binding code at:

(George Shepherd's Windows Forms FAQ)
http://www.syncfusion.com/FAQ/WinForms/

Michael Lang, MCSD

Frank said:
Hello,

i created an own DataGridColumn. In this class i get the corresponding data
with the GetColumnValueAtRow method. Now i will display all new lines in the
DataGrid in Red. So i inserted an additional field called 'New'.

My problem is that i cannot access the correct data row. I tried to access
the line with the following line of code:
NewFlag = (bool) ((DataRowView)
Source.Current).DataView.Table.Rows[RowNum].ItemArray[((DataRowView)
Source.Current).DataView.Table.Columns.IndexOf("New")];

But this works only, if the DataGrid is not sorted. In the other case the
row data are not identically with the row data from the GetColumnValueAtRow
method. So, if anybody knows, how i get the correct information, please
answer.

Thanks

Frank
 
Back
Top