使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据。
当需要在 Windows 窗体应用程序中显示表格数据时,请首先考虑使用 DataGridView 控件.若要以小型网格显示只读值,或者若要使用户能够编辑具有数百万条记录的表,DataGridView 控件将为您提供可以方便地进行编程以及有效地利用内存的解决方案(引自百度百科)。
以上很清楚的说明了DataGridView的作用及使用。
代码:
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString =
"Server=(local);Database=huli;Integrated Security=sspi";
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText =
"select userid as 医疗卡号,username as 姓名,p_jibing as 患病名称,p_chuanran as 传染,starttime as 入院时间,p_tiwen as 体温,p_huxi as 呼吸频率,p_xueyagao as 舒张压,p_xueyadi as 收缩压,p_zhusu as 主诉,p_xianbingshi as 现病史,p_zhenduanxinxi as 诊断信息,p_guomingshi as 过敏史 from tb_patient";
this.t = new DataTable();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
sqlConnection.Open();
sqlDataAdapter.Fill(this.t);
sqlConnection.Close ();
this.CourseViewByName = new DataView();
this.CourseViewByName.Table = this.t;
this.CourseViewByName.Sort = "姓名 ASC";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlCommand;
DataSet d = new DataSet();
da.Fill(d, "tb_patient");
dataGridView1.DataSource = d;
dataGridView1.DataMember = "tb_patient";
sqlConnection.Close();
代码:
DataRow searchResultRow = this.t.Rows.Find(this.txt_card.Text.Trim());
DataTable searchResultTable = this.t.Clone();
searchResultTable.ImportRow(searchResultRow);
this.dataGridView1.DataSource = searchResultTable;
DataRowView[] searchResultRowViews =
this.CourseViewByName.FindRows(this.txt_name.Text.Trim());
DataTable searchResultTable = this.t.Clone();
foreach (DataRowView dataRowView2 in searchResultRowViews)
{
searchResultTable.ImportRow(dataRowView2.Row);
}
this.dataGridView1.DataSource = searchResultTable;
原文:https://www.cnblogs.com/linyanfang1/p/9845959.html