逐行填充DataGridView仅填充最后一行 [英] Filling DataGridView row by row only populates last row

查看:187
本文介绍了逐行填充DataGridView仅填充最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个循环来填充一列整数的datagridview。我很确定我有循环和excel参考正确,因为我已经在其他项目中使用,但它不会正确显示。这是代码:

I'm trying to fill a datagridview with one column of integers using a loop. I'm pretty sure I've got the loop and excel reference correct, as I've used this on other projects, but it won't display correctly. Here's the code:

Dim da As OleDbDataAdapter
        Dim ds As DataSet

        xlWorkbook2 = xlApp.Workbooks.Open(FormFile)
        xlWsheet2 = xlWorkbook2.Sheets(sheetname)

        'open connection to nit spreadsheet'
        Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""" & FormFile & """;Extended Properties=""Excel 12.0;HDR=YES;""")

        'Fill dataviewgrid1 with element symbols, string'
        da = New OleDbDataAdapter("select * from [" & sheetname & "$A13:A" & lrow & "]", cn)
        ds = New System.Data.DataSet
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)

        'Fill dataviewgrid2 with compositions, integers'
        With xlWsheet2
            For xrow As Integer = 13 To lrow
                DataGridView2.ColumnCount = 1
                DataGridView2.Rows.Add()
                DataGridView2.Item(0, xrow - 12).Value = xlWsheet2.Cells(xrow, 2).Value
                Console.WriteLine(xlWsheet2.Cells(xrow, 2).Value)
            Next
        End With

所以dgv1填补罚款,没有问题。 Dgv2没有填充,或者取决于我获取正确的行数,但只填充最后一个单元格。因为我在那里有console.writeline命令,我看到代码是成功读取excel电子表格,所以这必须是一个显示的东西?我检查了大多数简单的显示选项,但是nothings似乎改变了?任何人都有任何想法?

So dgv1 fills fine, no problem there. Dgv2 does not fill, or rather I get the proper number of rows, but only the last cell is populated. Because I've got the console.writeline command in there, I see that the code IS reading the excel spreadsheet successfully, so this must be a display thing? I've checked most of the easy display options, but nothings seems to change? Anyone have any ideas?

推荐答案

我很确定你的datagridview有 AllowUserToAddRows 属性设置为 True 。如果是这样,每次使用 DataGridView2.Rows.Add()时,该行在最后一行之前添加;这是因为最后一行是用户手动添加一行到您的网格的那一行。但是在你的循环中,你总是编辑这一行。

I'm pretty sure your datagridview has the AllowUserToAddRows property set to True. If so, every time you use DataGridView2.Rows.Add(), that row is added before the last one; that's because the last row is the one the user uses to manually add a row to your grid. But in your loop you are always editing this one row.

如果你的datagridview将允许用户手动添加一行,你必须设置最后一行行。一个更干净的方法就是这样做:

If your datagridview is going to allow users to manually add a row, you have to set the value of the previous to last row. A cleaner way to do this would be like this:

Dim index as Integer
With xlWsheet2
    DataGridView2.ColumnCount = 1
    For xrow As Integer = 13 To lrow
        index = DataGridView2.Rows.Add()
        DataGridView2.Item(0, index).Value = xlWsheet2.Cells(xrow, 2).Value
        Console.WriteLine(xlWsheet2.Cells(xrow, 2).Value)
    Next
End With

或简单地

With xlWsheet2
    DataGridView2.ColumnCount = 1
    For xrow As Integer = 13 To lrow
        DataGridView2.Rows.Add(xlWsheet2.Cells(xrow, 2).Value)
        Console.WriteLine(xlWsheet2.Cells(xrow, 2).Value)
    Next
End With

(BTW,没有必要 DataGridView2.ColumnCount = 1 每个循环)

(BTW, there's no need to do DataGridView2.ColumnCount = 1 on each loop)

如果您决定更改,这两种方式仍然可以工作 AllowUserToAddRows False

Both of these ways will still work if you ever decide to change the AllowUserToAddRows to False.

这篇关于逐行填充DataGridView仅填充最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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