您的位置:首页 > 编程语言 > C#

C#Winform基础 显示datagridview有多少行,有多少列

2016-10-19 12:50 363 查看
镇场诗:
    清心感悟智慧语,不着世间名与利。学水处下纳百川,舍尽贡高我慢意。
    学有小成返哺根,愿铸一良心博客。诚心于此写经验,愿见文者得启发。
——————————————————————————————————————————

1 UI



2 keyCode

1         /// <summary>
2         /// 点击这个按钮出现一个对话框,显示有多少行
3         /// </summary>
4         /// <param name="sender"></param>
5         /// <param name="e"></param>
6         private void button1_Click(object sender, EventArgs e)
7         {
8             //这个数字不包括列标题的那一行
9             MessageBox.Show(dataGridView1.RowCount.ToString());
10         }
11
12
13         /// <summary>
14         /// 点击这个按钮出现一个对话框,显示有多少行
15         /// </summary>
16         /// <param name="sender"></param>
17         /// <param name="e"></param>
18         private void button2_Click(object sender, EventArgs e)
19         {
20             //不包含行标题的那一列
21             MessageBox.Show(dataGridView1.ColumnCount.ToString());
22         }


3 code

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10 using System.Xml.Linq;
11
12 namespace WindowsFormsApplication3
13 {
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20
21         private void Form1_Load(object sender, EventArgs e)
22         {
23             ShowXmlFile();
24         }
25
26         private void ShowXmlFile()
27         {
28             List<User> datas = new List<User>();
29             //form 窗体在加载的时候,读取xml文件
30             XDocument xmlFile = XDocument.Load("1.xml");
31             XElement root = xmlFile.Root;
32
33             foreach (var item in root.Elements())
34             {
35                 datas.Add(new User() { ShengHao = item.Element("ShengHao").Value, Password = item.Element("Password").Value });
36             }
37
38             //将list数据与datagridview绑定
39             dataGridView1.DataSource = datas;
40
41             //这行代码一开始是放在formload中的,但发现,每次增加新的数据之后,第一行都会被选中,所以
42             //把这行代码加入到showxmlfile中
43             dataGridView1.SelectedRows[0].Selected = false;
44
45         }
46
47         private void btnBook_Click(object sender, EventArgs e)
48         {
49
50             User newUser = new User() { ShengHao = textNewShengHao.Text, Password = textNewPassword.Text };
51
52             XDocument xmlFile = XDocument.Load("1.xml");
53             XElement root = xmlFile.Root;
54
55             //在根节点下新建一个子节点,名字叫“XianJia”
56             XElement newXianJia = new XElement("XianJia");
57             newXianJia.SetElementValue("ShengHao", newUser.ShengHao);
58             newXianJia.SetElementValue("Password", newUser.Password);
59             root.Add(newXianJia);
60
61             //保存
62             xmlFile.Save("1.xml");
63
64             //为何防止用户,双击啥的,出现一堆重复的数据,所以在这里出现一个messagebox
65             //我还发现,没有这个messagebox,虽然数据已经增加到了xml文件中,
66             //但是那个datagridview控件却不显示新的,但是在重新运行这个程序的时候会显示
67             //所以,一个messagebox贴心又省心
68             MessageBox.Show("欢迎回家");
69
70             //让datagirdview控件显示新的xml文件中的内容
71             ShowXmlFile();
72         }
73
74         private void deleteMenuStrip_Click(object sender, EventArgs e)
75         {
76
77         }
78
79         /// <summary>
80         /// 点击这个按钮出现一个对话框,显示有多少行
81         /// </summary>
82         /// <param name="sender"></param>
83         /// <param name="e"></param>
84         private void button1_Click(object sender, EventArgs e)
85         {
86             //这个数字不包括列标题的那一行
87             MessageBox.Show(dataGridView1.RowCount.ToString());
88         }
89
90
91         /// <summary>
92         /// 点击这个按钮出现一个对话框,显示有多少行
93         /// </summary>
94         /// <param name="sender"></param>
95         /// <param name="e"></param>
96         private void button2_Click(object sender, EventArgs e)
97         {
98             //不包含行标题的那一列
99             MessageBox.Show(dataGridView1.ColumnCount.ToString());
100         }
101     }
102 }


1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace WindowsFormsApplication3
8 {
9     /// <summary>
10     /// xml文件内容匹配类
11     /// </summary>
12     public class User
13     {
14         private string _shengHao;
15         private string _password;
16
17         public string Password
18         {
19             get
20             {
21                 return _password;
22             }
23
24             set
25             {
26                 _password = value;
27             }
28         }
29
30         public string ShengHao
31         {
32             get
33             {
34                 return _shengHao;
35             }
36
37             set
38             {
39                 _shengHao = value;
40             }
41         }
42     }
43 }


4 show





注:舍名利 喜欢看儒释道的书籍,所以举的例子不是普通的例子,但是效果都是一样的,还望能看得过去。

——————————————————————————————————————————
博文的精髓,在技术部分,更在镇场一诗。版本:VS2015 系统:Windows 7
C#是优秀的语言,值得努力学习。我是跟随 传智播客\黑马 .Net视频教程学习的。
如果博文的内容有可以改进的地方,甚至有错误的地方,请留下评论,我一定努力改正,争取铸成一个良心博客。
注:此文仅作为科研学习,如果我无意中侵犯了您的权益,请务必及时告知,我会做出改正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐