如何将选定的combobx值设置为Datagridview [英] How to set selected value of combobx to Datagridview
本文介绍了如何将选定的combobx值设置为Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的项目中有datagridview,combobox和bowse按钮。我将点击bowse按钮选择Excel文件.i从datagridview绑定到combobbox。我想从组合框的选定值显示datagridview。错误:在适配器上没有给出一个或多个必需参数的值.Fill(dt)。在我的代码中
I have datagridview ,combobox and bowse button in my project .I will choose Excel file on click bowse button .i was bind to combobbox from datagridview.I want to show datagridview from selected value of combobox. Error :"No value given for one or more required parameters " at adapter.Fill(dt) .In my code
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
DataGridView1.Columns("Column1").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
Try
Dim ofd As New OpenFileDialog
ofd.Title = "Please select the excel which you want to import"
ofd.Filter = "Excel files (*.xls)|*.xlsx"
If ofd.ShowDialog = DialogResult.OK Then
DataGridView1.Visible = True
filename = ofd.FileName
Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & filename & ";Extended Properties=""Excel 12.0;HDR=YES"";"
Dim con As OleDbConnection = New OleDbConnection(strin)
If con.State = ConnectionState.Closed Then
con.Open()
Dim command As OleDbCommand = New OleDbCommand()
command.CommandText = "Select * from [Sheet1$] "
command.Connection = con
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
adapter.SelectCommand = command
Dim dt As New DataSet
adapter.Fill(dt, "DT")
DataGridView1.DataSource = dt.Tables(0)
con.Close()
Bind(filename)
End If
Else
Exit Sub
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub Bind(ByVal strExcelFilePath As String)
Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strExcelFilePath & ";Extended Properties=""Excel 12.0;HDR=YES"";"
Dim con As OleDbConnection = New OleDbConnection(strin)
If con.State = ConnectionState.Closed Then
con.Open()
Dim command As OleDbCommand = New OleDbCommand()
command.CommandText = "Select Distinct Department from [Sheet1$] "
command.Connection = con
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
adapter.SelectCommand = command
Dim dt As New DataSet
adapter.Fill(dt, "DT")
ComboBox1.DisplayMember = dt.Tables(0).Columns(0).ColumnName
ComboBox1.DataSource = dt.Tables(0)
con.Close()
End If
End Sub
Public Sub ComboBind(ByVal strExcelFilePath As String)
Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strExcelFilePath & ";Extended Properties=""Excel 12.0;HDR=YES"";"
Dim con As OleDbConnection = New OleDbConnection(strin)
If con.State = ConnectionState.Closed Then
con.Open()
Dim command As OleDbCommand = New OleDbCommand()
command.CommandText = "Select Distinct Department from [Sheet1$] where Department=" & ComboBox1.Text
command.Connection = con
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
adapter.SelectCommand = command
Dim dt As New DataSet
'adapter.Fill(dt, "DT")
adapter.Fill(dt)
dt.Tables(0).Columns(0).ColumnName = ComboBox1.DisplayMember
DataGridView1.DataSource = dt.Tables(0)
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
' Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & filename & ";Extended Properties=""Excel 12.0;HDR=YES"";"
If ComboBox1.SelectedIndex = 0 Then
ComboBind(filename)
End If
End Sub
推荐答案
command.Connection = con
Dim adapter As OleDbDataAdapter = 新 OleDbDataAdapter()
adapter.SelectCommand = command
Dim dt As 新 DataSet
adapter.Fill(dt, DT)
DataGridView1.DataSource = dt.Tables( 0 )
con.Close()
Bind(filename)
End 如果
其他
退出 Sub
结束 如果
Catch ex As 异常
MsgBox( ex.ToString)
结束 尝试
结束 Sub
公开 Sub Bind( ByVal strExcelFilePath As String )
Dim strin As < span class =code-keyword> String = Provider = Microsoft.ACE.OLEDB.12.0;数据源=& strExcelFilePath& ;扩展属性=Excel 12.0; HDR = YES ;
Dim con As OleDbConnection = 新 OleDbConnection(strin)
如果 con.State = ConnectionState.Closed 然后
con.Open()
Dim 命令作为 OleDbCommand = 新 OleDbCommand()
command.CommandText = 从[Sheet1
" command.Connection = con Dim adapter As OleDbDataAdapter = New OleDbDataAdapter() adapter.SelectCommand = command Dim dt As New DataSet adapter.Fill(dt, "DT") DataGridView1.DataSource = dt.Tables(0) con.Close() Bind(filename) End If Else Exit Sub End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Public Sub Bind(ByVal strExcelFilePath As String) Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strExcelFilePath & ";Extended Properties=""Excel 12.0;HDR=YES"";" Dim con As OleDbConnection = New OleDbConnection(strin) If con.State = ConnectionState.Closed Then con.Open() Dim command As OleDbCommand = New OleDbCommand() command.CommandText = "Select Distinct Department from [Sheet1
command.Connection = con
Dim中选择Distinct Department adapter As OleDbDataAdapter = 新 OleDbDataAdapter()
adapter.SelectCommand = command
Dim dt As 新 DataSet
adapter.Fill(dt, DT)
ComboBox1.DisplayMember = dt.Tables( 0 )。列(< span class =code-digit> 0 )。ColumnName
ComboBox1.DataSource = dt.Tables( 0 )
con .Close()
结束 如果
结束 Sub
公开 < span class =code-keyword> Sub ComboBind( ByVal strExcelFilePath As 字符串)
Dim strin As 字符串 = Provider = Microsoft.ACE.OLEDB.12.0;数据源=& strExcelFilePath& ;扩展属性=Excel 12.0; HDR = YES ;
Dim con As OleDbConnection = 新 OleDbConnection(strin)
如果 con.State = ConnectionState.Closed 然后
con.Open()
Dim 命令作为 OleDbCommand = 新 OleDbCommand()
command.CommandText = 从[Sheet1
" command.Connection = con Dim adapter As OleDbDataAdapter = New OleDbDataAdapter() adapter.SelectCommand = command Dim dt As New DataSet adapter.Fill(dt, "DT") ComboBox1.DisplayMember = dt.Tables(0).Columns(0).ColumnName ComboBox1.DataSource = dt.Tables(0) con.Close() End If End Sub Public Sub ComboBind(ByVal strExcelFilePath As String) Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & strExcelFilePath & ";Extended Properties=""Excel 12.0;HDR=YES"";" Dim con As OleDbConnection = New OleDbConnection(strin) If con.State = ConnectionState.Closed Then con.Open() Dim command As OleDbCommand = New OleDbCommand() command.CommandText = "Select Distinct Department from [Sheet1
中选择Distinct Department,其中Department =& ComboBox1.Text
command.Connection = con
Dim adapter As OleDbDataAdapter = 新 OleDbDataAdapter()
adapter.SelectCommand = command
Dim dt As 新 DataSet
' adapter.Fill(dt,DT)
adapter.Fill(dt)
dt。表( 0 )。列( 0 )。ColumnName = ComboBox1.DisplayMember
DataGridView1.DataSource = dt.Tables( 0 )
结束 如果
结束 Sub
私有 Sub ComboBox1_SelectedIndexChanged( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 ComboBox1.SelectedIndexChanged
' Dim strin As String =Provider = Microsoft.ACE.OLEDB.12.0;数据源=& filename&;扩展属性=Excel 12.0; HDR = YES;
如果 ComboBox1.SelectedIndex = 0 那么
ComboBind (文件名)
结束 如果
< span class =code-keyword>结束 Sub
where Department=" & ComboBox1.Text command.Connection = con Dim adapter As OleDbDataAdapter = New OleDbDataAdapter() adapter.SelectCommand = command Dim dt As New DataSet 'adapter.Fill(dt, "DT") adapter.Fill(dt) dt.Tables(0).Columns(0).ColumnName = ComboBox1.DisplayMember DataGridView1.DataSource = dt.Tables(0) End If End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged ' Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & filename & ";Extended Properties=""Excel 12.0;HDR=YES"";" If ComboBox1.SelectedIndex = 0 Then ComboBind(filename) End If End Sub
这篇关于如何将选定的combobx值设置为Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文