i have to show data in datagrid view in windows forms
for that write code in class library as

public DataSet fillgrid()
        {
            string selectall = "select * from employee";
            con.Open();
            da = new SqlDataAdapter(selectall,con);
            da.Fill(ds,"employee");
            //con.Close();
            return ds;
            
           }

and iload add reference
and write code on form load as

Class1 c1 = new Class1();
 dataGridView1.DataSource = c1.fillgrid().Tables[0];

my problem is datagrid view is not showing table

Recommended Answers

All 2 Replies

try this it will work[/B]

private void GetData(string selectCommand)
{
    try
    {
        // Specify a connection string. Replace the given value with a 
        // valid connection string for a Northwind SQL Server sample
        // database accessible to your system.
        String connectionString =
            "Integrated Security=SSPI;Persist Security Info=False;" +
            "Initial Catalog=Northwind;Data Source=localhost";

        // Create a new data adapter based on the specified query.
        dataAdapter = new SqlDataAdapter(selectCommand, connectionString);

        // Create a command builder to generate SQL update, insert, and
        // delete commands based on selectCommand. These are used to
        // update the database.
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

        // Populate a new data table and bind it to the BindingSource.
        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        dataAdapter.Fill(table);
        bindingSource1.DataSource = table;

        // Resize the DataGridView columns to fit the newly loaded content.
        dataGridView1.AutoResizeColumns( 
            DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
    }

i have to show data in datagrid view in windows forms
for that write code in class library as

public DataSet fillgrid()
    {
        string selectall = "select * from employee";
        con.Open();
        da = new SqlDataAdapter(selectall,con);
        da.Fill(ds,"employee");
        //con.Close();
        return ds;

       }

and iload add reference
and write code on form load as

        Class1 c1 = new Class1();
        dataGridView1.DataSource = c1.fillgrid().Tables[0];

my problem is datagrid view is not showing table

I believe you should have to use the name you have assigned to table at the time of filling dataset with adapter.

Class1 c1 = new Class1();
dataGridView1.DataSource = c1.fillgrid().Tables["employee"];

because you can use index number 0, 1 and so on only when you haven't specified any name for table.

Hope this helps !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.