locked
Display datagrid when a Button is clicked RRS feed

  • Question

  • I have a datagrid which is already binded to a DB.
    how to display data on the datagrid when a button is clicked ?
    Thursday, December 13, 2012 9:50 AM

Answers

  • HI,

    on my end it is working fine . Have you checked , your datatable contains data ?

    This code is working on my end :

     DataTable dt = new DataTable();
                dt.Columns.Add("Col1");
                dt.Columns.Add("Col2");
    
                DataRow rd1 = dt.NewRow();
                rd1[0] = "abc"; rd1[1] = "abc1";
                dt.Rows.Add(rd1);
                dataGrid1.ItemsSource = dt.DefaultView;


    One good question is equivalent to ten best answers.

    • Marked as answer by BuBa1947 Thursday, December 13, 2012 11:15 AM
    Thursday, December 13, 2012 10:47 AM
  • Hi Buba1947

    add  ItemsSource="{Binding}" in xaml i.e

    <DataGrid AutoGenerateColumns="True" Grid.Column="1" Height="423" HorizontalAlignment="Left" Margin="22,24,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="591" ItemsSource="{Binding}" />

    hope it wil help u...

    let me know...


    Harshad..... Always 4 U

    • Marked as answer by BuBa1947 Thursday, December 13, 2012 11:15 AM
    Thursday, December 13, 2012 11:00 AM

All replies

  • Hi,

    you said it is already binded to DB , then it should display data .

    Is Autogeneratecolumn property trur or false ?

    Is ItemSource property of DataGrid set ?

    Can you show your xaml and code snippet for that datagrid !


    One good question is equivalent to ten best answers.


    • Edited by Shyam Kr Thursday, December 13, 2012 10:02 AM
    Thursday, December 13, 2012 10:00 AM
  • Sry... here is my code snippet

    public MainWindow()
            {

                InitializeComponent();
                try
                {
                    con = new SqlConnection(@"Data Source=Student;Initial Catalog=StudDB;Integrated Security=true;Persist Security Info=True;");
                    con.Open();
                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }

            private void button4_Click(object sender, RoutedEventArgs e)
            {
                label1.Content = DateTime.Now.ToString();
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                SqlDataAdapter da = null;
                DataSet ds = null;
                try
                {
                    da = new SqlDataAdapter("select * from Major",con);
                    da.SelectCommand.CommandTimeout = 100000;
                    ds = new DataSet();
                    da.Fill(ds);
                    dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
            }

    and my XAML :

    <DataGrid AutoGenerateColumns="True" Grid.Column="1" Height="423" HorizontalAlignment="Left" Margin="22,24,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="591" />
            <Button Content="Show Major" Grid.Column="1" Height="40" HorizontalAlignment="Left" Margin="768,96,0,0" Name="button1" VerticalAlignment="Top" Width="117" Click="button1_Click" />

                    
    Thursday, December 13, 2012 10:36 AM
  • I have set the  Autogeneratecolumn property = "true"

    and

     ItemSource property of DataGrid set  is 

    dataGrid1.ItemsSource = ds.Tables[0].DefaultView;

    but I get exception as 

    'The invocation of the constructor on type 'HMSystem.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.

    Thursday, December 13, 2012 10:40 AM
  • HI,

    on my end it is working fine . Have you checked , your datatable contains data ?

    This code is working on my end :

     DataTable dt = new DataTable();
                dt.Columns.Add("Col1");
                dt.Columns.Add("Col2");
    
                DataRow rd1 = dt.NewRow();
                rd1[0] = "abc"; rd1[1] = "abc1";
                dt.Rows.Add(rd1);
                dataGrid1.ItemsSource = dt.DefaultView;


    One good question is equivalent to ten best answers.

    • Marked as answer by BuBa1947 Thursday, December 13, 2012 11:15 AM
    Thursday, December 13, 2012 10:47 AM
  • My database has already tables contained in it. this code seems adding data on to the table dynamically.. this is not my question... I have already a table which is binded to the gridview. It already displays with the gridview, but now I added a button, so that if I click the button I would like to see the table on my datagrid...
    Thursday, December 13, 2012 10:56 AM
  • Hi Buba1947

    add  ItemsSource="{Binding}" in xaml i.e

    <DataGrid AutoGenerateColumns="True" Grid.Column="1" Height="423" HorizontalAlignment="Left" Margin="22,24,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="591" ItemsSource="{Binding}" />

    hope it wil help u...

    let me know...


    Harshad..... Always 4 U

    • Marked as answer by BuBa1947 Thursday, December 13, 2012 11:15 AM
    Thursday, December 13, 2012 11:00 AM
  • but I get exception as 

    'The invocation of the constructor on type 'HMSystem.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.

    This post may help you : http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/dd542b03-abed-469e-a6c2-a66221b99ff8


    One good question is equivalent to ten best answers.

    Thursday, December 13, 2012 11:05 AM
  • Thanks a lot @Harjohn and @bootstrap2

    I added it and it works fine.. 

    -Indhira

    Greetings from Germany 

    Thursday, December 13, 2012 11:13 AM
  • Hi BuBa1947

    if your problem get solved then please

    mark as answer so that it will be helpful for the others...

    Regards

    Harshad


    Harshad..... Always 4 U

    Thursday, December 13, 2012 11:15 AM
  • I did already :) 
    Thursday, December 13, 2012 11:16 AM