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

摘要:
2使用System.Text;45namespaceWindowsFormsDemo6{7usingSystem.Configuration;conn))33{34using(DataSeset=newDataSet())35{36ad.Fill(set);37this.dataGridView1.DataSource=set.Tables[0].DefaultView;

先上学习测试的一些截图

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

Winform的控件以及DataGridView的一般使用第1张

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

Winform的控件以及DataGridView的一般使用第2张

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

Winform的控件以及DataGridView的一般使用第3张

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

Winform的控件以及DataGridView的一般使用第4张

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

Winform的控件以及DataGridView的一般使用第5张

 6:全部代码

Winform的控件以及DataGridView的一般使用第6张Winform的控件以及DataGridView的一般使用第7张
  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},
下面就可以使用主键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(',')+ ",

cellCount=" + 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

免责声明:文章转载自《Winform的控件以及DataGridView的一般使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇编程语言 IDE 对比Quartz.NET简介下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

element-ui 树形表格多选

如题element-ui 2.13.2版本支持树形结构tabel,多层级折叠显示 但是没有多选 + 树形tabel, 业务需求的情况下必须要实现,操作勾选数据编辑 这里我们可以用两个事件来实现:   @select:用户勾选某行触发事件,第一个参数selection:所有选中的数据, 第二参数row:勾选的这行数据)   @select-all : 表头的...

PLSQL中查询数据的时候查询结果显示中文乱码(转)

原文链接:https://blog.csdn.net/hu1991die/article/details/45172311 1、在PLSQL中查询数据的时候查询结果显示中文乱码 2、需要在环境变量中新建两个环境变量: 第一个: 设置 NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK 第二个: 设置:LANG=zh_C...

AE加载不同数据的方法(GeoDatabase空间数据管理)

先看一下GeoDatabase核心结构模型图:     1  工作空间工厂WorkspaceFactory对象    WorkspaceFactory是GeoDatabase的入口,是一个抽象类,拥有很多子类,例如SdeWorkspaceFactory, AccessWorkspaceFactory, ShapfileWorkspaceFactory ...

5.多项式回归与模型泛化

(一)什么是多项式回归 还记得线性回归法吗?线性回归法有一个很大的局限性,那就是需要数据是有一定的线性关系的,但是现实中的很多数据是没有线性关系的。多项式就是用来解决这个问题的,可以处理非线性数据 在线性回归中,我们是假设数据具有线性关系的,因此我们在简单线性回归中,将直线的方向设置为y=ax+b的形式,那么我们求出a和b即可。 而对于有些数据,我们虽...

新浪股票接口AndroidSDK

昨天想到一个点子,需要访问股票行情。于是在网上搜了一下免费的股市行情的接口。发现新浪股票的数据接口比较稳定,于是就用它了。 网上对于新浪股票的数据接口介绍比较详细,并且实现也很简单,所以花了一下午就基本完成了。想到大家开发Android应用可能也会需要访问股市行情,特此将资料整理在此,并附上Javadoc文档和一个小Demo,以便后来人开发更为便捷。   ...

MySQL数据目录更改及相关问题解决方案

步骤相关 1、停掉MySQL服务 service mysql stop 2、把旧的数据目录/var/lib/mysql备份到新的数据目录/data/mysql cp /var/lib/mysql /data -R 3、给mysql组的mysql用户赋予新的数据目录的权限 chown -R mysql:mysql /data/mysql 4、修改my.cnf...