tags:

views:

92

answers:

4
+1  Q: 

Convert VB to C#

I need a little help converting some VB.NET code to C#. I have tried several "code converters" but none of them are giving me back a workable response.

Here's the code:

If Me.OrdersDataGridView.SelectedRows.Count > 0 Then
Dim editForm As New Order(Me.NorthwindDataSet, _
    Me.NorthwindDataSet.Orders.Rows.IndexOf_
    (CType(CType(Me.OrdersDataGridView.SelectedRows(0)._
    DataBoundItem, DataRowView).Row, NorthwindDataSet.OrdersRow)))
    editForm.Show()
End If

Any help with this is greatly appreciated!

EDIT: here's a link to the original article I found this in.

+6  A: 

Try this:

if (this.OrdersDataGridView.SelectedRows.Count > 0)
{
    NorthwindDataSet.OrdersRow row = (NorthwindDataSet.OrdersRow)
                                       ((DataRowView)this.OrdersDataGridView
                                            .SelectedRows(0).DataBoundIte).Row;

    Order editForm = new Order(
                           this.NorthwindDataSet,
                           this.NorthwindDataSet.Orders.Rows.IndexOf(row));

    editForm.Show();
}
Andrew Hare
+1 Perfect! Thank you.
Robert
Andrew I could be wrong but I think you have the VB style index for SelectedRows and you left off the m for DataBoundItem. But hopefully he can figure that out after trying it once and having it fail.
spinon
Spinon, you're right and I did. ;)
Robert
A: 
if (this.OrdersDataGridView.SelectedRows.Count > 0)
{
    Order editForm = new Order(this.NorthwindDataSet,
        this.NorthwindDataSet.Orders.Rows.IndexOf((NorthwindDataSet.OrdersRow)((DataRowView)this.OrdersDataGridView.SelectedRows[0].DataBoundItem).Row);
    editForm.Show();
}

Ok I think I typed that correct.

spinon
A: 

If code converters are failing here, it's likely because you need a reference in your Visual Studio solution to that specific database in order for it to work.

The conversion should be something like:

if (this.OrdersDataGridView.SelectedRows.Count > 0)
{
    var dataRowView = (DataRowView) this.OrdersDataGridView.SelectedRows(0).DataBoundItem;
    var ordersRow = (NorthwindDataSet.OrdersRow) dataRowView;
    var editForm = new Order(this.NorthwindDataSet, ordersRow)
    editForm.Show()
}
Billy ONeal
A: 
if (OrdersDataGridView.SelectedRows.Count > 0)
{
    var firstRowView = (DataRowView) OrdersDataGridView.SelectedRows[0].DataBoundItem;
    var firstOrderRow = (NorthwindDataSet.OrdersRow) firstRowView.Row;
    var editForm = new Order
    (
        this.NorthwindDataSet,
        this.NorthwindDataSet.Orders.Rows.IndexOf(firstOrderRow)
    );
    editForm.Show();
}
cHao