how to fill the datagrid with a table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monika varshney
    New Member
    • Nov 2008
    • 2

    #1

    how to fill the datagrid with a table

    I have a sql server table and i want to read it in the datagrid view of C# .Net.
    I think it is easy to do it. but i m not able to show any output in the datagrid...
    here is my code.please guide me.where i m wrong..

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    
    namespace grid
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataGrid dataGrid1;
                dataGrid1 = new DataGrid();
    
                string strConn, strSQL;
                strConn = "server=ABC,Database=test;user id = sa;password=manager;Trusted_Connection=Yes";
                strSQL = "select * from info";
    
                SqlDataAdapter da = new SqlDataAdapter(strSQL, strConn);
                DataSet ds = new DataSet();
                da.Fill(ds, "information");
    
                dataGrid1.DataMember = "info";
                dataGrid1.DataSource = ds;
           
            }      
    
        }
    }
    Last edited by kenobewan; Nov 10 '08, 12:02 PM. Reason: Use code tags
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Try datagrid1.datab ind(); and report back.

    Comment

    • krungkrung
      New Member
      • Nov 2008
      • 18

      #3
      Hi!

      I think, using the last code, dataGrid1.DataS ource = ds, you are refencing the whole dataset and not the specific table of the dataset which you named "information".. .maybe thats the reason why your datagridview doesnt know which particular records to show....

      you may try: dataGrid1.DataS ource = ds.Tables("info rmation");

      NOTE: information is the name of table you created in dataset during fill method execution of dataadapter [da.Fill(ds, "informatio n");]


      hope this helps...cheers (",)

      Comment

      • ruchikapali
        New Member
        • Oct 2008
        • 4

        #4
        HI!!!

        use

        datagrid1.datas ource=ds.tables["informatio n"];
        datagrid1.datab ind();

        Comment

        Working...