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的控件都了解甚少,所以处在学习中,写下博客主要是为了方便后期的回顾,当然也可以给一些新人第一次接触时做为学习的参考,以 ...
随机推荐
- django10-form表单组件
1.form组件的主要功能 生成页面的HTML标签和样式 ,将前端form表单的代码放在后端生成!! 对用户提交的数据进行校验(正则) 自动生成错误信息 保留上次输入信息 2.form组件常用字段与插 ...
- QT信号槽连接语法总结
信号槽是 Qt 框架引以为豪的机制之一. 所谓信号槽,实际就是观察者模式.当某个事件发生之后,比如,按钮检测到自己被点击了一下,它就会发出一个信号(signal).这种触发是没有目的的,类似广播.如果 ...
- Python3使用线程
Python2标准库中提供了两个模块thread和threading支持多线程.thread有一些缺陷在Python3中弃用,为了兼容性,python3 将 thread 重命名为 "_th ...
- 【XML】XPath表达式
XPath简介 XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言. XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力.起 ...
- 使用Spring访问Mongodb的方法大全——Spring Data MongoDB
1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...
- RSA 登陆加密与解密
最近公司项目验收后,客户请来的信息安全技术人员对我们的网站进行了各种安全测试与排查问题,其中就有一个登陆时的加密问题.本来如果只是单纯的加密,可以直接在前台用MD5加密,将加密的值添加到数据库即可.但 ...
- mysql里字符集的配置
[client]default-character-set=utf8[mysqld]character-set-server = utf8[mysql]default-character-set=ut ...
- you have new mail in /var/spool/mail/root !
今天开发的同事告诉我,他在登录系统时老是提示you have new mail in /var/spool/mail/root ! 我一猜就知道他们肯定又自己写定时任务了,这样的事已经发生过好几回了, ...
- 如何下载安装MySQL 解压版和安装版以及2个版本的区别
参考链接:https://blog.csdn.net/qq_33800083/article/details/80722829
- Mondb
1. MongoDB简介 • MongoDB是为快速开发互联网Web应用而设计的数据库系统.• MongoDB的设计目标是极简.灵活.作为Web应用栈的一部分.• MongoDB的数据模型是面向文档的 ...