首页 > 数据库技术 > 详细

SQL学习笔记之DataGridView学习

时间:2018-10-24 21:28:05      阅读:163      评论:0      收藏:0      [点我收藏+]

SQL学习笔记之DataGridView学习

:DataGridView介绍

使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据。

: DataGridView的使用

当需要在 Windows 窗体应用程序中显示表格数据时,请首先考虑使用 DataGridView 控件.若要以小型网格显示只读值,或者若要使用户能够编辑具有数百万条记录的表,DataGridView 控件将为您提供可以方便地进行编程以及有效地利用内存的解决方案(引自百度百科)

以上很清楚的说明了DataGridView的作用及使用。

三:思维导图

技术分享图片

 

四:举例

(1)载入数据到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();

技术分享图片

 

(2)搜索DataGridView中数据

代码:

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;

技术分享图片

 

SQL学习笔记之DataGridView学习

原文:https://www.cnblogs.com/linyanfang1/p/9845959.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!