vs C#控件使用:[2]DataGridView控件(二)

 时间:2024-10-19 14:58:24

1、建立WinForm项目,大伙都会,就不说过程了。然后将DataGridView控件拖到WinForm窗体中,呈现灰色(在第一篇中有讲解步骤)。

2、点击DataGridView控件右上角的箭头,然后选择“编辑列”。

vs C#控件使用:[2]DataGridView控件(二)vs C#控件使用:[2]DataGridView控件(二)

3、在出现的“编辑列”界面中,点击添加。

vs C#控件使用:[2]DataGridView控件(二)

4、在“添加”界面中,编辑“名称”和“页眉文本”。“名称”会在后台代码中引用,最好设置成英文;“页眉文本”是DataGridView控件中显示的列名。

vs C#控件使用:[2]DataGridView控件(二)

5、照此方法,分别添加:姓名、性别、年龄三列,然后点击完成。你会看到DataGridView控件中已经出现新添加的三列,表示添加成功。

vs C#控件使用:[2]DataGridView控件(二)vs C#控件使用:[2]DataGridView控件(二)

6、添加一个按钮(Button控件,此控件在左侧工具箱中),将其拖到WinForm窗体中。

vs C#控件使用:[2]DataGridView控件(二)vs C#控件使用:[2]DataGridView控件(二)

7、右键Button控件,选择“属性”,弹出属性窗口。

vs C#控件使用:[2]DataGridView控件(二)

8、修改Button控件的显示文本。在属性窗口中找到Text属性,将其从“button1”修改为“添加数据”,Button控件的显示文本就变为“添加数据”

vs C#控件使用:[2]DataGridView控件(二)vs C#控件使用:[2]DataGridView控件(二)vs C#控件使用:[2]DataGridView控件(二)

9、双击按钮,转到代码编辑界面,在此编写代码。

vs C#控件使用:[2]DataGridView控件(二)

10、编写给DataGridView控件添加数据的代码。private void button1_Click(object sender, EventArgs e){ //添加数据时,类型都是默认的字符串类型 string name = "Jim"; string gender = "男"; string age = "18"; //将上面三个变量合成一个数组 string[] row = { name, gender, age }; //给dataGridView1控件添加数据 dataGridView1.Rows.Add(row); }

vs C#控件使用:[2]DataGridView控件(二)

11、运行程序(调试),在显示的界面中点击按钮,DataGridView控件成功显示数据。

vs C#控件使用:[2]DataGridView控件(二)vs C#控件使用:[2]DataGridView控件(二)
  • Windows terminal 放大和缩小显示文本
  • 在 Windows 10 的 Edge 浏览器中禁用 Flash
  • vs启用非托管代码调试
  • c#如何设置button的Enter焦点事件
  • 如何使python语言中的pandas模块中的astype方法
  • 热门搜索
    白俄罗斯旅游 厦门旅游住宿 丽水旅游景点大全 张家界旅游费用 凤阳旅游 阿拉善旅游 延庆旅游景点 四川旅游路线 广西桂林旅游攻略 黄果树瀑布旅游