学习Winform的控件DataGridView的一般使用

先上学习测试的一些截图

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

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

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

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

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

6:全部代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 using System.Windows.Forms;
  9
 10 namespace WindowsFormsDemo
 11 {
 12     using System.Data;
 13     using System.Data.SqlClient;
 14     using System.Configuration;
 15
 16     public partial class Form1 : Form
 17     {
 18         private static readonly string connectionstr = ConfigurationManager.ConnectionStrings["hydb"].ConnectionString;
 19
 20         public Form1()
 21         {
 22             InitializeComponent();
 23         }
 24
 25         private void Form1_Load(object sender, EventArgs e)
 26         {
 27             // TODO: 这行代码将数据加载到表“huayaDBDataSet.K_City”中。您可以根据需要移动或删除它。
 28             this.k_CityTableAdapter.Fill(this.huayaDBDataSet.K_City);
 29             for (int i = 1; i < 1000; i++)
 30             {
 31                 this.progressBar1.Value = (int)(((i + 1) / 1000.0) * 100);
 32                 Application.DoEvents();
 33             }
 34             BingDataGridview();
 35         }
 36
 37         private void BingDataGridview()
 38         {
 39             using (SqlConnection conn = new SqlConnection(connectionstr))
 40             {
 41                 conn.Open();
 42                 using (SqlDataAdapter ad = new SqlDataAdapter("select ID,userID,userno ,Optext,Remark from F_OperateLog", conn))
 43                 {
 44                     using (DataSet set = new DataSet())
 45                     {
 46                         ad.Fill(set);
 47                         this.dataGridView1.DataSource = set.Tables[0].DefaultView;//绑定数据
 48                         dataGridView1.MultiSelect = false;//单选
 49                         dataGridView1.Rows[1].Selected=true;//默认第二行为选中的状态
 50                     }
 51                 }
 52             }
 53         }
 54
 55         private void btnSubmit_Click(object sender, EventArgs e)
 56         {
 57             string textboxStr = this.texboxStr.Text;
 58             string comboxStr = this.comboBox1.Text;
 59             string radiobtnStr = this.radioButton1.Checked == true ? "男" : "女";
 60             string textChekboxStr = string.IsNullOrEmpty(this.checkBox1.Text) == true ? "" : this.checkBox1.Text;
 61             string textChekboxStr2 = string.IsNullOrEmpty(this.checkBox2.Text) == true ? "" : this.checkBox2.Text;
 62
 63             string msg = $"textboxStr={textboxStr},comboxStr={comboxStr},radiobtnStr={radiobtnStr},textChekboxStr={ textChekboxStr},textChekboxStr2={textChekboxStr2}";
 64             MessageBox.Show(msg, "结果是:");
 65         }
 66
 67         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
 68         {
 69             string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
 70             if (!string.IsNullOrEmpty(id))
 71             {
 72                 // MessageBox.Show($"获取到主键ID={id}");
 73                 labshowid.Text = $"获取到主键ID={id}";
 74             }
 75         }
 76         /// <summary>
 77         /// 获取选中行的key ID
 78         /// </summary>
 79         /// <param name="sender"></param>
 80         /// <param name="e"></param>
 81         private void btnSelectID_Click(object sender, EventArgs e)
 82         {
 83             string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
 84             MessageBox.Show($"dataGridView1.CurrentRow.Cells[0].Value.ToString={id},\n下面就可以使用主键ID的值来CURD的操作");
 85         }
 86         /// <summary>
 87         /// 获取选中行的所有数据
 88         /// </summary>
 89         /// <param name="sender"></param>
 90         /// <param name="e"></param>
 91         private void btnSelectRowData_Click(object sender, EventArgs e)
 92         {
 93
 94             int rowIndex = dataGridView1.CurrentRow.Index;//选中当前行的索引
 95             int cellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);//获取一行的列有多少个
 96
 97             StringBuilder sb = new StringBuilder();
 98             for (int i = 0; i < cellCount; i++)
 99             {
100                 sb.Append(dataGridView1.CurrentRow.Cells[i].Value.ToString() + ",");
101             }
102             MessageBox.Show(sb.ToString().TrimEnd(‘,‘)+ ",\n\ncellCount=" + cellCount+ ",rowIndex=" + rowIndex);
103         }
104     }
105 }

原文地址:https://www.cnblogs.com/Fengge518/p/12040281.html

时间: 2024-07-31 22:59:32

学习Winform的控件DataGridView的一般使用的相关文章

C# winform 分页控件 datagridview列宽度自适应

版权归原作者(伍华聪)所有. VS2019 .Net FrameWork 4.7.2 1.修改按钮上文字,调整按钮大小.修改按钮和文本框字体为Microsoft Sans Serif, 9pt”,字体电脑中都有,可以尽量水平和垂直居中. 2.因为没有文档,导出excel总不成功,取消掉了导出按钮. 3.修改分页控件水平居中. 下载 视频 4.datagridview列宽度自适应 using System; using System.Collections.Generic; using Syste

类似web风格的 Winform 分页控件

背景 最近做一个Winform的小程序,需要用到分页,由于之前一直在用 TonyPagerForWinForm.dll ,但该库没有源代码,网上找的也不全面,索性就准备自己改造一个.在园子里翻了一下,发现路过秋天在多年前写了个分页控件,Winform 通用分页控件实战篇(提供源码下载).站在大神的肩膀上就是快,一会就改好了. 效果图 功能比较齐全,不过样式上,楼主十分喜欢easyUI或ext的列表分页风格.于是换了几个按钮,添加了图标,看看现在的效果. 控件源码在正文最下方. 使用简介 就那么一

WinForm Control 控件命名规范

1.标准控件 序号 控件类型简写 控件类型 1 btn Button 2 chk CheckBox 3 ckl CheckedListBox 4 cmb ComboBox 5 dtp DateTimePicker 6 lbl Label 7 llb LinkLabel 8 lst ListBox 9 lvw ListView 10 mtx MaskedTextBox 11 cdr MonthCalendar 12 icn NotifyIcon 13 nud NumeircUpDown 14 pi

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

一般情况下,我们都倾向于使用一个组织比较好的独立界面来录入或者展示相关的数据,这样处理比较规范,也方便显示比较复杂的数据.不过在一些情况下,我们也可能需要直接在GridView表格上直接录入或者修改数据,这种对于字段比较少,而且内容相对比较简单的情况下,效率是比较高的一种输入方式.本篇随笔主要介绍在DevExpress程序中使用GridView直接录入数据并保存的实现,以及使用Winform分页控件来进行数据直接录入的实现操作. 1.在GridView上展示数据 在GridView上展示数据,只

Android学习Scroller(三)——控件平移划过屏幕 (Scroller简单使用)

MainActivity如下: package cc.cn; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; /** * Demo描述: * Scroller使用示例--让控件平移划过屏幕 * * 参考资料: * http://blog.cs

C# Winform WindowsMediaPlayer控件

要做一个视频无缝切换的程序,所谓无缝就是在一个视频结束时立即开始另一个视频,中间不要有切换的黑屏 实现思路是放两个wmp播放控件,其中每个时刻只有一个在播放,另外一个处于暂停状态,并隐藏 当一个视频播放完后,切换显示另一个视频,同时这个视频隐藏起来并加载下一段视频,视频加载完成后(wmp会有事件通知)暂停 这样就去除了加载时的短暂空挡 有时可能会有键盘鼠标事件影响wmp的状态,所以使用定时器虽然监测wmp的播放状态 private void Form1_Load(object sender, E

winform分页控件

五一过的一多半已经过去了,感觉过的真快. 言归正传说说最近一直在用的winform分页控件. 一. 添加新项-用户控件 二.拖控件,这个很简单 三.写代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Win

winform用户控件

用途用户控件包含Time控件和一个lable控件,一个ToolStrip控件,每隔一秒显示一次时间     1. 生成用户控件   新建一个项目类型为用户控件   注意定义类名,此类名为以后工具箱中显示的名字,暂且定义此处类名为LabelTime. 文件名称为UserControl1.cs(无关紧要,vs引用dll的时候,都是关注类名非文件名)     namespace myWindowsFormsControlLibrary1 { public partial class labelTime

十三、Android学习笔记_Andorid控件样式汇总

<!-- 设置activity为透明 --> <style name="translucent"> <item name="android:windowBackground">@color/translucent</item> <item name="android:windowIsTranslucent">true</item> <item name="an