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的控件都了解甚少,所以处在学习中,写下博客主要是为了方便后期的回顾,当然也可以给一些新人第一次接触时做为学习的参考,以 ...
随机推荐
- .NET MVC5简介(一)
就像是.NET Framework WebApi与.NET Core WebApi一样,.NET Framework MVC与.NET Core MVC的区别,也是框架的之间的区别.本系列先首先从.N ...
- qt构建错误: dependent "*.h" does not exist.
项目中需要维护一套qt工程,今天发现一个头文件名称中单词拼写错误,就改正了,结果重新构建提示: dependent "*.h" does not exist. 原因:修改了文件后, ...
- Saltstack_使用指南08_远程执行-返回程序
1. 主机规划 salt 版本 [root@salt100 ~]# salt --version salt (Oxygen) [root@salt100 ~]# salt-minion --versi ...
- CDH预警配置QQ邮箱
一. 在QQ邮箱中开启POP   二 .关闭主机的sendmail,开启postfix (本机若没有两个服务,就需要先安装)本地安装sendmail和postfix [root@Slave1 ~] ...
- 编译 lineageos 14.1 on OnePlus3
breakfast oneplus3 命令的主要作用是 1.执行 vendorsetup.sh 2. variant 默认设置为 userdebug 3. 根据机型判断 调用 lunch cm 或 ...
- ARM 寻址方式
寻址方式有 9种 1.寄存器 2.立即数 3.寄存器位移 4.寄存器间接 5.基址 6.多寄存器 7.堆栈 8.块拷贝 9.相对 1. MOV R1,R2 R1 = R2 2. MOV R0,#0x1 ...
- 第50 课C++对象模型分析——成员变量(上)
C++对象模型,其实就是C++中的对象在内存中是如何排布的.C++中的对象包含了成员变量和成员函数,其实就是研究C++中的类对象它的成员变量和成员函数在内存中是如何排布的. 回归本质class 是一种 ...
- 第十三周博客作业 <西北师范大学| 周安伟>
第十三周助教作业 助教博客链接https://home.cnblogs.com/u/zaw-315/ 本周点评作业数量无 本周点评困难的博客作业无 本周工作情况制作实验七 团队作业3的千帆图和成绩排名 ...
- [C1] 线性回归(Linear Regression)
线性回归(Linear Regression with One / Multiple Variable) 定义符号(Symbol Definition) m = 数据集中训练样本的数量 n = 特征的 ...
- 用Python写Verilog(非HLS)
https://blog.csdn.net/qq_32010099/article/details/81197171 前段时间玩Python的时候好奇, 既然Python这么强大, 那么能不能用Pyt ...