0
这是一个C#窗口形成应用并正在从数据库作为数据读取器对象以显示它在图中所示的数据网格视图中获取数据,但我不能 表的大小设置为适合数据网格视图。
下面的代码:你想要的列宽为AutoSize
private void ViewTable()
{
SqlConnection con = new SqlConnection("Data Source=Abdullah-PC;Initial Catalog=SmartPharmacyDB;Integrated Security=True");
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "select drugname , companyname, price, instock, expirationdate from drugtab order by drugname";
con.Open();
SqlDataReader dr = com.ExecuteReader();
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
dataGridView1.Columns.Add("Drug Name", "drugname");
dataGridView1.Columns.Add("Company Name", "companyname");
dataGridView1.Columns.Add("Price", "price");
dataGridView1.Columns.Add("In Stock", "instock");
dataGridView1.Columns.Add("Expires On", "expirationdate");
int i = 0;
while (dr.Read())
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = dr["drugname"];
dataGridView1.Rows[i].Cells[1].Value = dr["companyname"];
dataGridView1.Rows[i].Cells[2].Value = dr["price"];
dataGridView1.Rows[i].Cells[3].Value = dr["instock"];
dataGridView1.Rows[i].Cells[4].Value = dr["expirationdate"];
i++;
}
con.Close();
}
这正是我的意思,谢谢很多 – alabasi
请标记正确的答案。它可以帮助其他人 –