Winform的控件以及DataGridView的一般使用

先上学习测试的一些截图

1:获取多个控件上面的值(checkbox,combobox,textbox,radiobutton)

2:获取到选择行的主键ID的value,方便我们进一步CURD

3:获取选择一行的数据以及一行是多少列

4:绑定显示自定义的列头名称

5:选中一行的属性设置操作

 6:全部代码

  1 using System;
  2 using System.Text;
  3 using System.Windows.Forms;
  4 
  5 namespace WindowsFormsDemo
  6 {
  7     using System.Configuration;
  8     using System.Data;
  9     using System.Data.SqlClient;
 10 
 11     public partial class Form1 : Form
 12     {
 13         private static readonly string connectionstr = ConfigurationManager.ConnectionStrings["hydb"].ConnectionString;
 14 
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         private void Form1_Load(object sender, EventArgs e)
 21         {
 22             // TODO: 这行代码将数据加载到表“huayaDBDataSet.K_City”中。您可以根据需要移动或删除它。
 23             this.k_CityTableAdapter.Fill(this.huayaDBDataSet.K_City);
 24             BingDataGridview();
 25         }
 26 
 27         private void BingDataGridview()
 28         {
 29             using (SqlConnection conn = new SqlConnection(connectionstr))
 30             {
 31                 conn.Open();
 32                 using (SqlDataAdapter ad = new SqlDataAdapter("select ID,userID,userno ,Optext,Remark from F_OperateLog", conn))
 33                 {
 34                     using (DataSet set = new DataSet())
 35                     {
 36                         ad.Fill(set);
 37                         this.dataGridView1.DataSource = set.Tables[0].DefaultView;//绑定数据
 38                         dataGridView1.MultiSelect = false;//单选
 39                         dataGridView1.Rows[1].Selected=true;//默认第二行为选中的状态
 40                     }
 41                 }
 42             }
 43         }
 44 
 45         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
 46         {
 47             string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
 48             if (!string.IsNullOrEmpty(id))
 49             {
 50                 // MessageBox.Show($"获取到主键ID={id}");
 51                 labshowid.Text = $"获取到主键ID={id}";
 52             }
 53         }
 54         /// <summary>
 55         /// 获取选中行的key ID
 56         /// </summary>
 57         /// <param name="sender"></param>
 58         /// <param name="e"></param>
 59         private void BtnSelectID_Click(object sender, EventArgs e)
 60         {
 61             string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
 62             MessageBox.Show($"dataGridView1.CurrentRow.Cells[0].Value.ToString={id},\n下面就可以使用主键ID的值来CURD的操作");
 63         }
 64         /// <summary>
 65         /// 获取选中行的所有数据
 66         /// </summary>
 67         /// <param name="sender"></param>
 68         /// <param name="e"></param>
 69         private void BtnSelectRowData_Click(object sender, EventArgs e)
 70         {
 71 
 72             int rowIndex = dataGridView1.CurrentRow.Index;//选中当前行的索引
 73             int cellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);//获取一行的列有多少个
 74 
 75             StringBuilder sb = new StringBuilder();
 76             for (int i = 0; i < cellCount; i++)
 77             {
 78                 sb.Append(dataGridView1.CurrentRow.Cells[i].Value.ToString() + ",");
 79             }
 80             MessageBox.Show(sb.ToString().TrimEnd(',')+ ",\n\ncellCount=" + cellCount+ ",rowIndex=" + rowIndex);
 81         }
 82 
 83         private void BtnProStart_Click(object sender, EventArgs e)
 84         {
 85             for (int i = 1; i < 100; i++)
 86             {
 87                 this.progressBar1.Value = (int)(((i + 1) / 100.0) * 100);
 88                 Application.DoEvents();
 89                 int miao = new Random().Next(1, 10);
 90                 System.Threading.Thread.Sleep(miao * 10);
 91             }
 92             MessageBox.Show("数据加载成功!", "请稍后", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
 93         }
 94 
 95         private void BtnSubmit_Click(object sender, EventArgs e)
 96         {
 97             string textboxStr = this.texboxStr.Text;
 98             string comboxStr = this.comboBox1.Text;
 99             string radiobtnStr = this.radioButton1.Checked == true ? "" : "";
100             string textChekboxStr = string.IsNullOrEmpty(this.checkBox1.Text) == true ? "" : this.checkBox1.Text;
101             string textChekboxStr2 = string.IsNullOrEmpty(this.checkBox2.Text) == true ? "" : this.checkBox2.Text;
102 
103             string msg = $"textboxStr={textboxStr},comboxStr={comboxStr},radiobtnStr={radiobtnStr},textChekboxStr={ textChekboxStr},textChekboxStr2={textChekboxStr2}";
104             MessageBox.Show(msg, "结果是:");
105         }
106     }
107 }
View Code
posted @ 2019-12-14 17:22  天天向上518  阅读(921)  评论(0编辑  收藏  举报