先上学习测试的一些截图

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的一般使用的更多相关文章

  1. winform 分页控件

    http://www.cnblogs.com/liuyunsheng/p/4853387.html http://www.cnblogs.com/wuhuacong/archive/2011/07/0 ...

  2. winform基础控件总结

    转自:http://www.cnblogs.com/top5/archive/2010/04/29/1724039.html 基础 - 常用控件 C# WinForm开发系列 - CheckBox/B ...

  3. [原创][开源]SunnyUI.Net, C# .Net WinForm开源控件库、工具类库、扩展类库、多页面开发框架

    SunnyUI.Net, 基于 C# .Net WinForm 开源控件库.工具类库.扩展类库.多页面开发框架 Blog: https://www.cnblogs.com/yhuse Gitee: h ...

  4. 在DevExpress程序中使用Winform分页控件直接录入数据并保存

    一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数 ...

  5. winform窗体控件(全)

    回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...

  6. winform基本控件----按钮

    这次来引用一个我们上课时候老师给的一个实验内容,来说一下winform程序设计中的按钮控件的使用.下面是我们老师给的实验内容. 实验目的: 掌握Winform的开发环境. 掌握窗体的创建和基本方法. ...

  7. WinForm给控件加入hint文字

    本文代码主要是参考别人的,仅为个人记录,方面后续使用~ 效果图: 主要代码在一个Win32Utility类中,代码如下: public static class Win32Utility { [Dll ...

  8. C# WinForm实现控件拖动实例介绍

    主要是设计控件的MouseDown.MouseLeave.MouseMove事件.一步步来吧:1.定义一个枚举类型,描述光标状态 private enum EnumMousePointPosition ...

  9. Winform DevExpress控件库(一) DevExpress控件库的安装与新建第一个DevExpress项目

    前言:因为这段时间要接触到DevExpress控件库,而我本身甚至对winform的控件都了解甚少,所以处在学习中,写下博客主要是为了方便后期的回顾,当然也可以给一些新人第一次接触时做为学习的参考,以 ...

随机推荐

  1. Python笔记:设计模式之代理模式

    代理通常就是一个介于寻求方和提供方之间的中介系统.其核心思想就是客户端(寻求方)没有直接和提供方(真实对象)打交道,而是通过代理对象来完成提供方提供的资源或操作. 代理其实就是封装实际服务对象的包装器 ...

  2. FCC---Change Animation Timing with Keywords--两个小球从A都B,相同循环时间 duration, 不同的速度 speed

    In CSS animations, the animation-timing-function property controls how quickly an animated element c ...

  3. Java的包

    Java 包 Java面向对象的核心的概念:类.接口.抽象类.对象:[主体] 包的定义: 指的是一个程序的目录,在最早的时候,如果要开发一个程序,只需要定义一个Java文件,而后在这个文件中编写所需要 ...

  4. Linux下设置root密码

    如下面的代码所示: sudo passwd [sudo] geeksong 的密码: 输入新的 UNIX 密码: 重新输入新的 UNIX 密码: passwd:已成功更新密码 更性的unix密码就是r ...

  5. [Linux] centos6.5升级安装的supervisor

    因为我要用它来管理多个PHP进程去执行任务,在默认版本下的配置多个子进程不起作用 默认版本的supervisor版本比较低2.1.9,对于运行多个子进程貌似有问题,最新版的supervisor4.1的 ...

  6. MQTT linux centOS7 部署

    系统版本centos7 X64 1.设置保存安装包路径 # cd /usr/local/src 2.开始下载源包 官网资源: https://mosquitto.org/files/source/ # ...

  7. v-bind是是否需要绑定某一个类名

    v-bind 结合 css样式 结合标签显示是否要显示出某个样式 <p :class="['bg','dx', {'lin':falg}]">{{ msg }}< ...

  8. [C3W2] Structuring Machine Learning Projects - ML Strategy 2

    第二周:机器学习策略(2)(ML Strategy(2)) 误差分析(Carrying out error analysis) 你好,欢迎回来,如果你希望让学习算法能够胜任人类能做的任务,但你的学习算 ...

  9. bcftools

    beftools非常复杂,大概有20个命令,每个命令下面还有N多个参数 annotate .. edit VCF files, add or remove annotations call .. SN ...

  10. ajax.readyState和HTTP状态码的提示

    ajax.readyState 0 -(未初始化)还没有调用send()方法 1 -(载入)已调用send()方法,正在发送请求 2 -(载入完成)send()方法执行完成,已经接收到全部响应内容 3 ...