Winform的控件以及DataGridView的一般使用
先上学习测试的一些截图
1:获取多个控件上面的值(checkbox,combobox,textbox,radiobutton)

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

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

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

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

6:全部代码
using System;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsDemo
{
using System.Configuration;
using System.Data;
using System.Data.SqlClient; public partial class Form1 : Form
{
private static readonly string connectionstr = ConfigurationManager.ConnectionStrings["hydb"].ConnectionString; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“huayaDBDataSet.K_City”中。您可以根据需要移动或删除它。
this.k_CityTableAdapter.Fill(this.huayaDBDataSet.K_City);
BingDataGridview();
} private void BingDataGridview()
{
using (SqlConnection conn = new SqlConnection(connectionstr))
{
conn.Open();
using (SqlDataAdapter ad = new SqlDataAdapter("select ID,userID,userno ,Optext,Remark from F_OperateLog", conn))
{
using (DataSet set = new DataSet())
{
ad.Fill(set);
this.dataGridView1.DataSource = set.Tables[].DefaultView;//绑定数据
dataGridView1.MultiSelect = false;//单选
dataGridView1.Rows[].Selected=true;//默认第二行为选中的状态
}
}
}
} private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string id = dataGridView1.CurrentRow.Cells[].Value.ToString();
if (!string.IsNullOrEmpty(id))
{
// MessageBox.Show($"获取到主键ID={id}");
labshowid.Text = $"获取到主键ID={id}";
}
}
/// <summary>
/// 获取选中行的key ID
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnSelectID_Click(object sender, EventArgs e)
{
string id = dataGridView1.CurrentRow.Cells[].Value.ToString();
MessageBox.Show($"dataGridView1.CurrentRow.Cells[0].Value.ToString={id},\n下面就可以使用主键ID的值来CURD的操作");
}
/// <summary>
/// 获取选中行的所有数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnSelectRowData_Click(object sender, EventArgs e)
{ int rowIndex = dataGridView1.CurrentRow.Index;//选中当前行的索引
int cellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);//获取一行的列有多少个 StringBuilder sb = new StringBuilder();
for (int i = ; i < cellCount; i++)
{
sb.Append(dataGridView1.CurrentRow.Cells[i].Value.ToString() + ",");
}
MessageBox.Show(sb.ToString().TrimEnd(',')+ ",\n\ncellCount=" + cellCount+ ",rowIndex=" + rowIndex);
} private void BtnProStart_Click(object sender, EventArgs e)
{
for (int i = ; i < ; i++)
{
this.progressBar1.Value = (int)(((i + ) / 100.0) * );
Application.DoEvents();
int miao = new Random().Next(, );
System.Threading.Thread.Sleep(miao * );
}
MessageBox.Show("数据加载成功!", "请稍后", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
} private void BtnSubmit_Click(object sender, EventArgs e)
{
string textboxStr = this.texboxStr.Text;
string comboxStr = this.comboBox1.Text;
string radiobtnStr = this.radioButton1.Checked == true ? "男" : "女";
string textChekboxStr = string.IsNullOrEmpty(this.checkBox1.Text) == true ? "" : this.checkBox1.Text;
string textChekboxStr2 = string.IsNullOrEmpty(this.checkBox2.Text) == true ? "" : this.checkBox2.Text; string msg = $"textboxStr={textboxStr},comboxStr={comboxStr},radiobtnStr={radiobtnStr},textChekboxStr={ textChekboxStr},textChekboxStr2={textChekboxStr2}";
MessageBox.Show(msg, "结果是:");
}
}
}
Winform的控件以及DataGridView的一般使用的更多相关文章
- winform 分页控件
http://www.cnblogs.com/liuyunsheng/p/4853387.html http://www.cnblogs.com/wuhuacong/archive/2011/07/0 ...
- winform基础控件总结
转自:http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html 基础 - 常用控件 C# WinForm开发系列 - CheckBox/B ...
- [原创][开源]SunnyUI.Net, C# .Net WinForm开源控件库、工具类库、扩展类库、多页面开发框架
SunnyUI.Net, 基于 C# .Net WinForm 开源控件库.工具类库.扩展类库.多页面开发框架 Blog: https://www.cnblogs.com/yhuse Gitee: h ...
- 在DevExpress程序中使用Winform分页控件直接录入数据并保存
一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数 ...
- winform窗体控件(全)
回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...
- winform基本控件----按钮
这次来引用一个我们上课时候老师给的一个实验内容,来说一下winform程序设计中的按钮控件的使用.下面是我们老师给的实验内容. 实验目的: 掌握Winform的开发环境. 掌握窗体的创建和基本方法. ...
- WinForm给控件加入hint文字
本文代码主要是参考别人的,仅为个人记录,方面后续使用~ 效果图: 主要代码在一个Win32Utility类中,代码如下: public static class Win32Utility { [Dll ...
- C# WinForm实现控件拖动实例介绍
主要是设计控件的MouseDown.MouseLeave.MouseMove事件.一步步来吧:1.定义一个枚举类型,描述光标状态 private enum EnumMousePointPosition ...
- Winform DevExpress控件库(一) DevExpress控件库的安装与新建第一个DevExpress项目
前言:因为这段时间要接触到DevExpress控件库,而我本身甚至对winform的控件都了解甚少,所以处在学习中,写下博客主要是为了方便后期的回顾,当然也可以给一些新人第一次接触时做为学习的参考,以 ...
随机推荐
- Zuul 1.x 的工作原理
Zuul简介 Zuul在微服务架构中,可以作为提供动态路由,监控,弹性,安全等边缘服务的框架.在Netflix,被用作所有请求到达streaming application的前门.Zuul使用一系列不 ...
- 微服务(入门学习五):identityServer4+ocelot+consul实现简单客户端模式
简介 主要是采用identity Server4 和ocelot 加上consul 实现简单的客户端模式 开发准备 环境准备 下载并安装Consul具体请参考前几篇的内容 项目介绍 创建ocelot ...
- PHP代码篇(七)--PHP及MySQL已经使用过的函数
一.PHP常用函数 //数组转字符串 $str = implode(',',$device_string); //字符串转数组 $arr = explode(',',$device_string); ...
- shell脚本模板----自动生成开头注释信息
每当我们新建一个shell脚本都要去写一些繁琐的注释信息,这会浪费掉我们很多的时间,有没有感觉很痛苦呢? 哈哈 下面给大家分享一个shell脚本的模板文件,把它拷贝到用户的家目录下并命名成 .vim ...
- linux 安装PostgreSQL12
一.安装步骤 1.设置保存安装包的目录 # cd /usr/local/src 2.开始下载源包 # wget https://ftp.postgresql.org/pub/source/v12.1/ ...
- mean shift聚类算法的MATLAB程序
mean shift聚类算法的MATLAB程序 凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. mean shift 简介 mean shift, 写的 ...
- SpringCloud学习笔记(五、SpringCloud Netflix Hystrix)
目录: Hystrix简介 线程隔离:线程池.信号量 服务降级.服务熔断.请求缓存.请求合并 Hystrix完整流程.Hystrix属性值 注解方式实现Hystrix Hystrix Dashboar ...
- 【CSP-S 2019】D2T1 Emiya 家今天的饭
Description 传送门 Solution 算法1 32pts 爆搜,复杂度\(O((m+1)^n)\) 算法2 84pts 裸的dp,复杂度\(O(n^3m)\) 首先有一个显然的性质要知道: ...
- PageHelper分页(十)
分页有两种: (1) 物理分页:物理分页依赖的是某一物理实体,这个物理实体就是数据库,比如MySQL数据库提供了limit关键字,程序员只需要编写带有limit关键字的SQL语句,数据库返回的就是分页 ...
- 正睿暑期培训day4考试
链接 A 求出来到每座山的距离后,就可以计算出每只猫等待的时间与出发时间的关系. 如果出发时间为\(x\),求出来只猫的等待时间.这里用\(b_i\)表示第i只猫的等待时间.然后我们将这些时间排序.问 ...