Help w/ winform DataGrid to DataGridView transition (best practice?)

F

frostbb

Greetings,

We're starting to transition our legacy C# apps from earlier versions to
VS2008 versions.

Many of our apps allow the user to select a row from a DataGrid into a 'Data
Edit' area composed of Text, Combo, etc boxes that contain the individual
columns of the DataGrid row.

To extract information from the user selected DataGrid row we use the
following code ....

Given a DataGrid / DataGridView named "dgResults"

string sSelectedDirID = null;
string sSelectedGridID = null;
DataRowView oDrvSelectedGridRow = null;
try
{
oDrvSelectedGridRow =
(DataRowView)this.BindingContext[dgResults.DataSource,
dgResults.DataMember].Current;
sSelectedDirID = oDrvSelectedGridRow["Dir_id"].ToString().Trim();
sSelectedGridID = oDrvSelectedGridRow["Grid_id"].ToString().Trim();
}
catch { return;}

The best translation of the code above for a DataGridView seems to be
something like

string sSelectedDirID = null;
string sSelectedGridID = null;
DataRowView oDrvSelectedGridRow = null;
try
{
oDrvSelectedGridRow = this.dgResults.CurrentRow.DataBoundItem AS
DataRowView;
sSelectedDirID = oDrvSelectedGridRow["Dir_id"].ToString().Trim();
sSelectedGridID = oDrvSelectedGridRow["Grid_id"].ToString().Trim();
}
catch { return;}

Anyone have a better approach ?

Thanks in advance.

Barry in Oregon.
 
Z

Zhi-Xin Ye [MSFT]

Dear Barry,

To extract information from the user selected DataGridView row, we can
access the value directly without accessing the data source, for example,

string sSelectedDirID =
this.dataGridView1.CurrentRow.Cells["Dir_id"].Value.ToString();
string sSelectedGridID =
this.dataGridView1.CurrentRow.Cells["Grid_id"].Value.ToString();

Should you have any questions, please don't hesitate to let me knonw.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
F

frostbb

Mr Ye (again I hope this is the correct form of address)

Thank you! Much better than having to tag the binding manager!

Barry in Oregon
 
Z

Zhi-Xin Ye [MSFT]

Dear Barry,

I'm glad that my suggestion helped!
And, yes, the address form you used is correct, my family name is Ye, and
I'm male. :)

Have a nice day!

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top