标签:abs 选中行 自定义 box static form sed 方便 windows
先上学习测试的一些截图
1:获取多个控件上面的值(checkbox,combobox,textbox,radiobutton)
2:获取到选择行的主键ID的value,方便我们进一步CURD
3:获取选择一行的数据以及一行是多少列
4:绑定显示自定义的列头名称
5:选中一行的属性设置操作
6:全部代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 using System.Windows.Forms; 9 10 namespace WindowsFormsDemo 11 { 12 using System.Data; 13 using System.Data.SqlClient; 14 using System.Configuration; 15 16 public partial class Form1 : Form 17 { 18 private static readonly string connectionstr = ConfigurationManager.ConnectionStrings["hydb"].ConnectionString; 19 20 public Form1() 21 { 22 InitializeComponent(); 23 } 24 25 private void Form1_Load(object sender, EventArgs e) 26 { 27 // TODO: 这行代码将数据加载到表“huayaDBDataSet.K_City”中。您可以根据需要移动或删除它。 28 this.k_CityTableAdapter.Fill(this.huayaDBDataSet.K_City); 29 for (int i = 1; i < 1000; i++) 30 { 31 this.progressBar1.Value = (int)(((i + 1) / 1000.0) * 100); 32 Application.DoEvents(); 33 } 34 BingDataGridview(); 35 } 36 37 private void BingDataGridview() 38 { 39 using (SqlConnection conn = new SqlConnection(connectionstr)) 40 { 41 conn.Open(); 42 using (SqlDataAdapter ad = new SqlDataAdapter("select ID,userID,userno ,Optext,Remark from F_OperateLog", conn)) 43 { 44 using (DataSet set = new DataSet()) 45 { 46 ad.Fill(set); 47 this.dataGridView1.DataSource = set.Tables[0].DefaultView;//绑定数据 48 dataGridView1.MultiSelect = false;//单选 49 dataGridView1.Rows[1].Selected=true;//默认第二行为选中的状态 50 } 51 } 52 } 53 } 54 55 private void btnSubmit_Click(object sender, EventArgs e) 56 { 57 string textboxStr = this.texboxStr.Text; 58 string comboxStr = this.comboBox1.Text; 59 string radiobtnStr = this.radioButton1.Checked == true ? "男" : "女"; 60 string textChekboxStr = string.IsNullOrEmpty(this.checkBox1.Text) == true ? "" : this.checkBox1.Text; 61 string textChekboxStr2 = string.IsNullOrEmpty(this.checkBox2.Text) == true ? "" : this.checkBox2.Text; 62 63 string msg = $"textboxStr={textboxStr},comboxStr={comboxStr},radiobtnStr={radiobtnStr},textChekboxStr={ textChekboxStr},textChekboxStr2={textChekboxStr2}"; 64 MessageBox.Show(msg, "结果是:"); 65 } 66 67 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 68 { 69 string id = dataGridView1.CurrentRow.Cells[0].Value.ToString(); 70 if (!string.IsNullOrEmpty(id)) 71 { 72 // MessageBox.Show($"获取到主键ID={id}"); 73 labshowid.Text = $"获取到主键ID={id}"; 74 } 75 } 76 /// <summary> 77 /// 获取选中行的key ID 78 /// </summary> 79 /// <param name="sender"></param> 80 /// <param name="e"></param> 81 private void btnSelectID_Click(object sender, EventArgs e) 82 { 83 string id = dataGridView1.CurrentRow.Cells[0].Value.ToString(); 84 MessageBox.Show($"dataGridView1.CurrentRow.Cells[0].Value.ToString={id},\n下面就可以使用主键ID的值来CURD的操作"); 85 } 86 /// <summary> 87 /// 获取选中行的所有数据 88 /// </summary> 89 /// <param name="sender"></param> 90 /// <param name="e"></param> 91 private void btnSelectRowData_Click(object sender, EventArgs e) 92 { 93 94 int rowIndex = dataGridView1.CurrentRow.Index;//选中当前行的索引 95 int cellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);//获取一行的列有多少个 96 97 StringBuilder sb = new StringBuilder(); 98 for (int i = 0; i < cellCount; i++) 99 { 100 sb.Append(dataGridView1.CurrentRow.Cells[i].Value.ToString() + ","); 101 } 102 MessageBox.Show(sb.ToString().TrimEnd(‘,‘)+ ",\n\ncellCount=" + cellCount+ ",rowIndex=" + rowIndex); 103 } 104 } 105 }
标签:abs 选中行 自定义 box static form sed 方便 windows
原文地址:https://www.cnblogs.com/Fengge518/p/12040281.html