3

I am creating a WPF Page and I want to expose the data resulting from a SQL query on it using DataGrid. I use C# and SqlDataAdapter. The relevant code of the query in the code behind file is:

string sqlStr2 = "SELECT Conference_Name, Year FROM ....";
SqlDataAdapter dAdapt2 = new SqlDataAdapter(sqlStr2, cnStr);
DataSet dataSet2 = new DataSet();
dAdapt2.Fill(dataSet2);

The data derived from the query must be inserted into two columns. However, I cannot manage to bind them on the XAML file. Here is the XAML code:

<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top"
    HorizontalAlignment="Left">
    <DataGrid Name="dtg1" AutoGenerateColumns="False"
        RowHeaderWidth="0" ItemsSource="{Binding Path=dataSet2}"
        Margin="0,0,0,-23">
        <DataGrid.Columns>
            <DataGridTextColumn Width="110" Header="Conference"
                Binding="{Binding Path=Conference_Name}"/>
        <DataGridTextColumn Width="110" Header="Year"
                Binding="{Binding Path=Year}"/>
    </DataGrid.Columns>
</DataGrid>

The data are not visible when I run the program. What is wrong? Should I declare a source in the header lines of the XAML file?

Toni
  • 1,555
  • 4
  • 15
  • 23
arjacsoh
  • 8,932
  • 28
  • 106
  • 166
  • datatSet2 needs to be a Public Property and the DataContext of the Window needs to be the code behind. – paparazzo Feb 15 '12 at 14:46
  • What does your code behind look like as well..? can you create a method and in the load event call a method named BindData() for example then create that method BindData() and put your code there for the DataAdapter – MethodMan Feb 15 '12 at 14:57

3 Answers3

1

You need to select a table or view for the DataGrid to bind to. A DataSet is a collection of DataTables. See this related SO post on how to bind a DataSet to the WPF DataGrid.

Community
  • 1
  • 1
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
0

You need to bind the DataContext here is an example of how I would do it in code behind for example to bind to a Listbox Replace your example here with what is posted below

//string sqlStr2 = "SELECT Conference_Name, Year FROM ....";
//SqlDataAdapter dAdapt2 = new SqlDataAdapter(sqlStr2, cnStr);
//DataSet dataSet2 = new DataSet();
//dAdapt2.Fill(dataSet2);

form load 
{ 
   call BindData();
} //this is sudu code.. 

private void BindData()
{
    DataSet dtSet = new DataSet();
    using (connection = new SqlConnection(connectionString))
    {
        command = new SqlCommand(sql, connection);              
        SqlDataAdapter adapter = new SqlDataAdapter();          
        connection.Open();
        adapter.SelectCommand = command;
        adapter.Fill(dtSet, "Customers");
        listBox1.DataContext = dtSet;
    }

}

use this link as a good example or us MSDN and Google Search How to bind a table in a dataset to a WPF datagrid in C# and XAML

Community
  • 1
  • 1
MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

Like @silverninja wrote you have to bind to the DataTable, and like always you need the right DataContext. I wish you would use MVVM. What I don't get is why you use binding when you do all in code behind.

The ugly code behind:

public class Window1: Window
{
    private void Anymethod()
    {
        //just some code pieces
        string sqlStr2 = "SELECT Conference_Name, Year FROM MyTable";
        SqlDataAdapter dAdapt2 = new SqlDataAdapter(sqlStr2, cnStr);
        DataSet dataSet2 = new DataSet();
        dAdapt2.Fill(dataSet2);
        this.dtg1.ItemsSource = dataSet2.Tables["MyTable"].DefaultView;
    }
}

XAML without binding, because you do not need it:

<DataGrid Name="dtg1" AutoGenerateColumns="False" RowHeaderWidth="0"
          Margin="0,0,0,-23">
    <DataGrid.Columns>
        <DataGridTextColumn  Width="110"  Header="Conference"
            Binding="{Binding Path=Conference_Name}" />
        <DataGridTextColumn Width="110" Header="Year"
            Binding="{Binding Path=Year}" />
    </DataGrid.Columns>
</DataGrid>

Notes:

  • Code is handwritten.
  • You should really look into MVVM makes stuff much clearer.
  • If you wanna mix up code behind and binding you simply have to the set the right DataContext and bind to the public property of your DataTable.
Toni
  • 1,555
  • 4
  • 15
  • 23
blindmeis
  • 22,175
  • 7
  • 55
  • 74