C# 将网格控件与SQL Server数据库链接
我是WPF和C的新手。我有一个包含行和列的网格,其中一列用于显示从数据库行获得的值(文本框用于收集数据)。这是我写的代码,但似乎不起作用C# 将网格控件与SQL Server数据库链接,c#,wpf,data-binding,grid,C#,Wpf,Data Binding,Grid,我是WPF和C的新手。我有一个包含行和列的网格,其中一列用于显示从数据库行获得的值(文本框用于收集数据)。这是我写的代码,但似乎不起作用 SqlConnection connection = new SqlConnection("Data Source=DESKTOP-QTFGN00;Database=VITALS;Integrated Security=True"); static void RetrieveMultipleResults(SqlConnection connection)
SqlConnection connection = new SqlConnection("Data Source=DESKTOP-QTFGN00;Database=VITALS;Integrated Security=True");
static void RetrieveMultipleResults(SqlConnection connection)
{
using (connection)
{
SqlCommand command = new SqlCommand("SELECT Height, Weight FROM dbo.Vitals;", connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.HasRows)
{
textBox.MeasurementGrid = (reader["Weight"].ToString());
textBox.DataBind();
textBox_Copy.Text = (reader["Height"].ToString());
while (reader.Read())
{
textBox.Text = (reader["Weight"].ToString());
textBox_Copy.Text = (reader["Height"].ToString());
}
reader.NextResult();
}
}
}
如何将从数据库读取的值绑定到特定的文本框?例如,数据库中的“重量”需要显示在测量网格中的
textBox
和textBox\u Copy
中的“高度” 您需要的是一个数据网格。此控件将负责为您生成列和行
您可以将行集合简单地分配给Datagrid的“ItemsSource”属性
我还建议您习惯模型-视图-模型(MVVM)模式