学习笔记(2019.4.8)控件输入\输出

本文详细介绍了C#窗体应用中RadioButton、PictureBox、DateTimePicker、DataGridView及ComboBox五种常用控件的使用方法,包括如何通过SQL进行数据交互、图片加载等关键操作。

主要介绍RadioButton,PictureBox,DataTimePicker,DataGridView,ComboBox这五种控件的输入和输出。

思维导图:   

如下

示例代码

如下:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("select*from tb_Information where CardNo=@CardNo", conn);
            SqlCommand cmd2 = new SqlCommand("select*from tb_KS", conn);
            cmd.Parameters.AddWithValue("@CardNo", "0000000001");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();                                           //声明并实例化SQL数据适配器,同时借助构造函数,将其SelectCommand属性设为先前创建的SQL命令;
            sqlDataAdapter.SelectCommand = cmd2;                                                      //将SQL数据适配器的查询命令属性指向SQL命令;
            DataTable KSTable = new DataTable();                                                         //声明并实例化数据表,用于保存所有班级,以用作下拉框数据源;                                                                           //打开SQL连接;
            sqlDataAdapter.Fill(KSTable);                                                                //SQL数据适配器读取数据,并填充班级数据表;
            combo_KS.DataSource = KSTable;                                                         //将班级下拉框的数据源设为班级数据表;
            combo_KS.DisplayMember = "KSName";                                                          //将班级下拉框的显示成员设为班级数据表的名称列;
            combo_KS.ValueMember = "KSID";
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                CardNo_text.Text = dr["CardNo"].ToString();
                ZhuyuanNo_text.Text = dr["ZhuyuanNo"].ToString();
                BinganNo_text.Text = dr["BinganNo"].ToString();
                Name_text.Text = dr["Name"].ToString();
                radio_nan.Checked = (bool)dr["Gender"];
                radio_nv.Checked = !(bool)dr["Gender"];
                IDCard_text.Text = dr["IDCard"].ToString();
                ZD_text.Text = dr["ZhuyuanZD"].ToString();
                dtp_BirthDate.Value = (DateTime)dr["BirthDate"];
                combo_KS.SelectedValue = (int)dr["KSID"];
                combo_Marriaged.Text = dr["Marriaged"].ToString();
                combo_QK.Text = dr["ZhuyuanQK"].ToString();
                combo_BQ.Text = dr["BQ"].ToString(); 
            }

private void btn_Openphoto_Click(object sender, EventArgs e)
        {
            OpenFileDialog openPhotoDialog = new OpenFileDialog()                                           //声明并实例化打开文件对话框;
            {                                                                                               //在初始化器中,设置打开文件对话框的各属性;
                Title = "打开照片文件"                                                          //对话框标题;
            ,
                Filter = "BMP Files (*.jpg)|*.jpg"                                                        //文件格式过滤器;
            ,
                InitialDirectory = @"C:\"                                                                 //初始目录;
            };
            if (openPhotoDialog.ShowDialog() == DialogResult.OK)                                            //显示打开文件对话框,若打开文件对话框的对话结果为点击OK键;
            {
                string PhotoFileName = openPhotoDialog.FileName;                                              //将对话框获得的文件名,存入本窗体的私有字段中;
                Photo.Image = Image.FromFile(PhotoFileName);                                  //调用图像的静态方法FromFile从指定文件中读取图像,并赋予图像框;
            }
        }
    }


 

效果截图 

如下:

RadioButton截图,Gender字段设为bit类型,在调用时用bool进行判断

DateTimePicker截图

PictureBox截图,注意,要在原先的表创建一个Photo字段,设为允许空值,并用文件流的方式存储读取

ComboBox截图,注意,ComboBox必须再创建一个单独的有科室和编号的表,读取时SqlDataReader读取原先表的信息,sqlDataAdapter存储新创建的表的信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值