locked
Dynamically populate the grid RRS feed

  • Question

  • Hi all,

    I am using c sharp and in a combo box I displaying table names. When the user select a one table I want to display that table data dynamically in the grid.Is it possible to do this? How can I search that table in the database and do this? Please help me,

    Thank you.

    Tuesday, May 10, 2011 11:15 AM

Answers

  • Connect To The Database

    1. Create a SqlConnection object specifying your Connection string details that you have in configuration settings.

    2. Create a SqlCommand object and provide your Sql statment and the connection string.

    3. You can then use a SqlAdapter to get the DataTable which contains the table data that you want.

     

    Populating the Grid

    1. Use the .Net grid, DataGridView and you can associate the DataTable that you get as the source for the DataGridView.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx gives you a very good example of doing this.

    Wednesday, May 11, 2011 5:04 AM

All replies

  • How do you interact with the database? Do you use ADO.NET classes or LINQ to sql or may be another ORM? There is a million of options to implement described case but start point is needed.

     

    For example you can implement it through the datasets.

     

    First you need to get a list of databases (use SqlCommand here).

     

    You also need a grid that is bound to the DataSet. Set auto-population of the grid's columns to true and fill your DataSet (not typed) using combobox's  selected value.

    Tuesday, May 10, 2011 12:20 PM
  • Hi Ekaterina,

    I am using sqlexpress and I have a database with multiple tables. I prefer to implement using datasets. But I have doubt with how to open the required table and populate details in the grid according to the combo box selection. I am connecting to the database using configuration settings.

    thank for the reply

    regards

    Wednesday, May 11, 2011 3:06 AM
  • Connect To The Database

    1. Create a SqlConnection object specifying your Connection string details that you have in configuration settings.

    2. Create a SqlCommand object and provide your Sql statment and the connection string.

    3. You can then use a SqlAdapter to get the DataTable which contains the table data that you want.

     

    Populating the Grid

    1. Use the .Net grid, DataGridView and you can associate the DataTable that you get as the source for the DataGridView.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx gives you a very good example of doing this.

    Wednesday, May 11, 2011 5:04 AM
  •  

    Hi,

     

    Has your issue been resolved? Would you mind letting us know the result of the suggestions?

     

    Now I will mark an answer, you can mark others that you think to be so useful to your issue.

    You could “Unmark As Answer” if it doesn’t make sense.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, May 22, 2011 4:49 AM