Darsh
Darsh

Reputation: 73

what is the error in this code datagridview?

SqlConnection con = new SqlConnection("Data Source=MOSTAFA;Initial Catalog=mohasba;Integrated Security=True");
SqlDataAdapter SDA = new SqlDataAdapter("select * from قيد_اليوميه where رقم_القيد='" + textBox1.Text + "'", con);
con.Open();
DataTable DT = new DataTable();
SDA.Fill(DT);
dataGridView1.DataSource = SDA;

When I run this code the the datagridview appear Empty

Upvotes: 1

Views: 3915

Answers (4)

LuisR9
LuisR9

Reputation: 116

Change the DataSource to DT.

This:

dataGridView1.DataSource = SDA;

To This:

dataGridView1.DataSource = DT;

Upvotes: 0

Asmodeo
Asmodeo

Reputation: 43

you missed the dataGridView.dataBind(); this will Bind your data with the control

dataGridView1.DataSource = SDA;

dataGridView1.dataBind();

Upvotes: 0

iefpw
iefpw

Reputation: 7042

Make sure the DataGridView has automatic column generation property set. If the datagridview is set to manual generation of column, it won't display the data. You have to map the columns to the sql column manually.

  1. make sure the query returns data
  2. make sure datagridview has its automatic property set. If it doesn't work delete the datagridview and put a new datagridview and don't change any information about the datagridview. Default behavior is automatic data column generation, which should display the data.

Upvotes: 0

fbueckert
fbueckert

Reputation: 412

If there's no error message, and the datagrid is empty, it's very likely in your SQL. Have you tried running it directly? Does it return results?

If that works, doublecheck the value of textbox1.

Upvotes: 1

Related Questions