6

I want to fill a dataGrid in my WPF application.

My XAML:

<DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" 
Margin="102,72,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="848" />

My code behind:

  public void FillGrid()
    {
        string MyConString =    
        "SERVER=myserver.com;" +
        "DATABASE=mydatabase;" +
        "UID=myuserid;" +
        "PASSWORD=mypass;";

        string sql = "SELECT clientnr, name, address FROM clients ORDER BY name";

        MySqlConnection connection = new MySqlConnection(MyConString);
        MySqlCommand cmdSel = new MySqlCommand(sql, connection);
        DataTable dt = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
        da.Fill(dt);
        dataGrid1.DataContext = dt;
    }

I'm sure that the MySQL part is correct, it does not give any errors. VS10 express doesn't give any errors. But if i execute the method my dataGrid won't be filled.

What I'm doing wrong?

Thanks in advance!

5 Answers 5

6

Set your DataGrid's binding:

<DataGrid ItemsSource="{Binding }" />
Sign up to request clarification or add additional context in comments.

2 Comments

@Lars See the update to set the DataGrid binding. Also, verify your FillGrid method is getting run.
@HaiderKhattak I'd suggest posting a new question with the relevant code. The most likely cause is your DataContext isn't set correctly.
4

You definitely want it to be bound to the DataTable and not the Adapter, as Rachel suggested (the adapter's job is to populate the DataTable). Also, it's good to enclose connections and commands in usings to make sure everything is cleaned up, like this:

public void FillGrid()
{
    string MyConString =
    "SERVER=myserver.com;" +
    "DATABASE=mydatabase;" +
    "UID=myuserid;" +
    "PASSWORD=mypass;";

    string sql = "SELECT clientnr, name, address FROM clients ORDER BY name";

    using (MySqlConnection connection = new MySqlConnection(MyConString))
    {
        connection.Open();
        using (MySqlCommand cmdSel = new MySqlCommand(sql, connection))
        {
            DataTable dt = new DataTable();
            MySqlDataAdapter da = new MySqlDataAdapter(cmdSel);
            da.Fill(dt);
            dataGrid1.DataContext = dt;
        }
        connection.Close();
    }
}

Comments

3

Replace

dataGrid1.DataContext = dt; 

with

dataGrid1.ItemsSource = dt.DefaultView;

1 Comment

This should explain why the replacement is recommended.
1

Just call the method FillGrid() after InitializeComponents() in your code behind. I just did that and it runs perfectly.

1 Comment

details pleaseeeee
1

I always use this code to display/fill data in my Datagrid:

SqlConnection con = new SqlConnection("your connection string");
    
private void LoadGrid()
    {
        SqlCommand cmd = new SqlCommand("Select * From XXX;", con);
        DataTable dt = new DataTable();
        con.Open();
        SqlDataAdapter sdr = new SqlDataAdapter(cmd);
        sdr.Fill(dt);
        dataGrid.ItemsSource = dt.DefaultView;
        con.Close();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.