Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

DoubleClick row in DataGrid Error

$
0
0

Here is the Code I am working with that another member posted to help me out but it is throwing an Error that I just cannot figure out.

Thank You for the help!

I am thinking it might have to do with the Xaml in some way so here it is:

<DataGrid Height="418"  VerticalAlignment="Top" IsReadOnly="True" Width="846" ItemsSource="{Binding Path=MyDataBinding}" Name="datagrid1" BorderBrush="#FF0C5900" Style="{DynamicResource DataGridStyle1}" Background="#FF1F1F1F" ColumnHeaderHeight="30" ColumnWidth="127" BorderThickness="1,0,0,1" SelectionMode="Single" RowHeight="40" FontSize="14"><DataGrid.RowStyle><Style TargetType="{x:Type DataGridRow}"><EventSetter Event="MouseDoubleClick" Handler="DataGrid_MouseDoubleClick"/></Style></DataGrid.RowStyle></DataGrid>

A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Column 'ToyInfo' does not belong to table MyDataBinding.

string ToyInfo = drv.Row["ToyInfo"].ToString();

Below is the entire block of code:

private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataGridRow dgr = sender as DataGridRow;
            DataRowView drv = dgr.DataContext as DataRowView;
            string toyId = drv.Row["ToyId"].ToString();

            conConnect = new SqlConnection("Data Source=GERRY;Initial Catalog=toyDB;Integrated Security=True");

            SqlCommand command = new SqlCommand("SELECT * FROM ToyInfo WHERE ToyId=@ToyId");
            command.Connection = conConnect;
            command.Parameters.AddWithValue("@ToyId", toyId);
            conConnect.Open();

            dAdapter = new SqlDataAdapter(command);
            DS = new DataSet();

            dAdapter.Fill(DS, "MyDataBinding");

            datagrid2.ItemsSource = DS.Tables[0].AsDataView();

            dAdapter.Dispose();
            conConnect.Close();
        }



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>