VB程序员博客

VB程序开发

有个问题!我要双击一个datagridview的第一行的时候,会出现一个form,这个form上面也有个datagridview并且绑定了excel数据!然后,双击第2行,要那个form又出现,但是datagridview上显示的是不同的数据,一直双击第3行,第4行,第5行,都会出现那个form,但datagridview上显示不同的数据!我是用的这个方法,做了5个form(我是新手,方法很笨,呵呵),每次点datagridview上不同的行的时候,就会分别出现!但这个方法好麻烦。因为这5个form都是一样的!只是每次打开的路径(红字显示的)不同罢了~!我想要只做一个form可以出现5次而显示不同的数据基于不同的数据路径~!!诚恳谢谢大家了!!很急~

Dim str As String
        Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection
        Dim fName As String
        fName = "c:SCOR2 ConfigurationLevel 2-Source.xls"
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fName & ";Extended Properties=""Excel 8.0;HDR=YES;"""
        con.Open()
        str = "select [No] from  [sheet1$] where predecessor like '%,%'"

5行分别打开5个form:

Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
        If e.RowIndex = 0 Then
            MessageBox.Show("this row is column name ,it can not be edited ,please select the other row")
        Else
            If e.RowIndex = 1 Then

                Form5.ShowDialog()
                Form5.Close()
            Else
                If e.RowIndex = 2 Then

                    Form4.ShowDialog()
                    Form4.Close()
                Else
                    If e.RowIndex = 3 Then

                        Form6.ShowDialog()
                        Form6.Close()
                    Else
                        If e.RowIndex = 4 Then

                            Form7.ShowDialog()
                            Form7.Close()
                        Else
                            If e.RowIndex = 5 Then

                                Form8.ShowDialog()
                                Form8.Close()

                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub

定义个全局变量lineindex
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
        If e.RowIndex = 0 Then
            MessageBox.Show("this row is column name ,it can not be edited ,please select the other row")
        Else
          lineindex=e.RowIndex
          form.show
        End If
    End Sub

将fname设置为数组,分别存为相应的excel文件路径
在form加载的时候设置文件名为fname(lineindex)

怎样将fname设置为数组存为相应的路径?
谢谢你啊!我初学有些东西还不怎么懂!

一个简单的办法,2个form,1主,另一个显示数据
public id as integer '记录点击的列
public function con(fname,sqlstr) ’连接数据库
Dim str As String
        Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection
        Dim fName As String
        fName = "c:SCOR2 ConfigurationLevel 2-Source.xls"
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fName & ";Extended Properties=""Excel 8.0;HDR=YES;"""
        con.Open()
        sqlstr = "select [No] from  [sheet1$] where predecessor like '%,%'"
end sub
然后加主窗体的datagridview_click加
id=得到当前id
数据窗体的form_load
select case id
case 1
    con(连接字符串,要执行的sql)
case 2
……
end select
然后把数据集挂到datagridview上。
另外补充一点,要及时关闭con

谢谢了哈~搞出来了。
我是定了个全局变量:
Public ExcelPath As String ' ExcelPath to open several files at different location
然后分别一个一个的传下去,这样貌似简单些。
If e.RowIndex = 1 Then
                Dim Frm As Form5 = New Form5()
                Frm.ExcelPath = "c:SCOR2 ConfigurationLevel 2-Plan.xls"  ' ExcelPath to open several files at different location
                Frm.ShowDialog()
            Else
                If e.RowIndex = 2 Then

                    Dim Frm As Form5 = New Form5()
                    Frm.ExcelPath = "c:SCOR2 ConfigurationLevel 2-Source.xls"
                    Frm.ShowDialog()
           
            …
            …
     


标签: , , ,