![](/img/trans.png)
[英]Getting Data from SQL Server to populate in DataGrid in C# WPF
[英]How to populate a datagrid view from a SQL Server database in C#
我修改了表,使其具有正確的記錄數,並在表上顯示了行數,但根本沒有填充表。
這是我的代碼:
public TableOutput()
{
InitializeComponent();
FillData();
}
void FillData()
{
string connectionString = @"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\14142\source\repos\ComputingProject\ComputingProject\computingProject.mdf; Integrated Security = True; Connect Timeout = 30";
using (SqlConnection c = new SqlConnection(connectionString))
{
c.Open();
using (SqlDataAdapter a = new SqlDataAdapter("SELECT Client, Cost, Description FROM jobs", c))
{
DataTable t = new DataTable();
a.Fill(t);
dataGridView1.DataSource = t;
dataGridView1.Update();
}
}
}
可能您尚未在gridview中聲明這些列。 因此,請在設置數據源之前嘗試以下操作:
dataGridView1.AutoGenerateColumns = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.