blah blah blah is here! blah blah » Close

up0down
link

This code connects fine to my SQL database, but the DataGridView doesn't display any of the information that I queried. Any ideas on what I'm lacking here?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DataGridUpdate
{
public partial class Form1 : Form
{
private SqlCommandBuilder cb;
private SqlDataAdapter da;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string connString = @"server=(local)\SQLExpress; integrated security = sspi; database = northwind;";
string sql = @"SELECT * FROM employees";
SqlConnection conn = new SqlConnection(connString);
SqlCommand sqlCommand1 = new SqlCommand(sql, conn);
da = new SqlDataAdapter();
da.SelectCommand = sqlCommand1;
cb = new SqlCommandBuilder(da);
da.Fill(dataSet1, "employees");
dataGridView1.DataBindings.Add("text", dataSet1, "employees");

}
private void button1_Click(object sender, EventArgs e)
{
da.Update(dataSet1, "employees");
}
}
}

last answered 2 years ago

1 answers

up0down
link

If you're wanting to bind the DGV to the "employees" table, then I'd replace this line:
dataGridView1.DataBindings.Add("text", dataSet1, "employees");
with:
dataGridView1.DataSource = dataSet1.Tables["employees"];

up0down
link

Perfect! Thanks again.

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback