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
請標記正確的答案。它可以幫助其他人 –