我如何以编程方式将行添加到datagridview [英] how can i add rows into a datagridview programmatically

查看:80
本文介绍了我如何以编程方式将行添加到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不使用数据绑定的情况下将数据集中的行添加到datagridview
请帮忙......



如果列数少,则下面的代码对我有帮助.但是如果列数更多,这不是一个好方法

I want to add rows from a dataset to datagridview with out using databinding
Please help...............



If there are few columns then The below code is Helping me . but in case of more columns this is not a goodway

dgClient.Columns.Add("client", "Client")
dgClient.Columns("client").Width = 150
dgClient.Columns.Add("comments","Comments")
dgClient.Columns("comments").Width = 250 
Dim i As IntegerFor i = 0 To dsClient.Tables("TableClient").Rows.Count - 1
dgClient.Rows.Add()
dgClient.Rows(i).Cells("client").Value = dsClient.Tables("TableClient").Rows(i).Item("Client")
dgClient.Rows(i).Cells("comments").Value = dsClient.Tables("TableClient").Rows(i).Item("Comments")
Next
dgClient.Columns.Add("client", "Client")
dgClient.Columns("client").Width = 150
dgClient.Columns.Add("comments", "Comments")
dgClient.Columns("comments").Width = 250

Dim i As Integer
For i = 0 To dsClient.Tables("TableClient").Rows.Count - 1
dgClient.Rows.Add()
dgClient.Rows(i).Cells("client").Value = dsClient.Tables("TableClient").Rows(i).Item("Client")
dgClient.Rows(i).Cells("comments").Value = dsClient.Tables("TableClient").Rows(i).Item("Comments")
Next

推荐答案

只需创建一个带有数据集的临时表并将其作为datagridview.

在数据表中,您可以一个一行地添加一行,并且可以在添加值时进行所需的修改.然后在创建之后,您只需要提供作为数据源.


或清除您的要求,为什么要这样.
Just create a temp Table with dataset and give this as datasource of a datagridview.

In datatable you can add row one by one and can do modification which you want while adding values. And then after creating you just have to give as datasource.


OR clear your requirement why you want like this.


希望获得帮助
将数据集中的行添加到datagridview
hope this help
add rows from dataset to datagridview


点击事件按以下顺序添加新按钮:
1.使用foreach循环,将网格的所有记录放入数据集中
2.向数据集添加一个空白行.
3.再次将数据集绑定到网格.
这将在网格中显示一个新行,而不会影响网格数据.
At click event of add new button do following sequence:
1. using foreach loop, put all the records of grid in a dataset
2. add one blank row to dataset.
3. bind dataset again to grid.
this will show a new row in grid without impacting grid data.


这篇关于我如何以编程方式将行添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆