从sql表填充datagridviewcomboboxcolum [英] populate datagridviewcomboboxcolum from sql table

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

问题描述

我正在尝试从sql表加载填充datagirdview组合框列,但它没有填充,也没有显示任何错误。请帮助我







私有子DataGridView1_CellClick(ByVal发送者为对象,ByVal e As System。 Windows.Forms.DataGridViewCellEventArgs)处理DataGridView1.CellClick

尝试



con.Open()





如果.Rows.Count = 0则退出Sub

Dim i As Integer

Dim cmdname As String =从tblname中选择名称

Dim daname1 As SqlDataAdapter = New SqlDataAdapter(cmdname,con)

Dim dsname1 As DataSet = New DataSet

daname1。填充(dsname1,tblname)



i = DataGridView1.CurrentRow.Index



Dim gridComboBox As New DataGridViewComboBoxCell

使用DataGridView1

.Item(2,i)= gridComboBox

gridComboBox.DataSource = dsname1.Tables(tblname)

gridComboBox.ValueMember =Name

gridComboBox.DisplayMember =Name





结束

con.Close()

Catch ex As Exception

如果MessageBox.Show(ex.Message)= DialogResult.OK然后

退出Sub

结束如果





结束尝试

结束Sub

I am trying load populate datagirdview combo box column from sql table but it is not populating and not showing any error also. Kindly help me pls



Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Try

con.Open()


If .Rows.Count = 0 Then Exit Sub
Dim i As Integer
Dim cmdname As String = "select Name from tblname"
Dim daname1 As SqlDataAdapter = New SqlDataAdapter(cmdname, con)
Dim dsname1 As DataSet = New DataSet
daname1.Fill(dsname1, "tblname")

i = DataGridView1.CurrentRow.Index

Dim gridComboBox As New DataGridViewComboBoxCell
With DataGridView1
.Item(2, i) = gridComboBox
gridComboBox.DataSource = dsname1.Tables("tblname")
gridComboBox.ValueMember = "Name"
gridComboBox.DisplayMember = "Name"


End With
con.Close()
Catch ex As Exception
If MessageBox.Show(ex.Message) = DialogResult.OK Then
Exit Sub
End If


End Try
End Sub

推荐答案

这些是绑定组合框的三种方法



试试这个..





使用系统;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing ;

使用System.Linq;

使用System.Text;

使用System.Windows.Forms;
使用System.Data.OleDb;

命名空间Dynamically_bind_data_to_combobox_from_database

{

公共部分类Form1:表格

{

string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings [dsn];

OleDbCommand com;

OleDbDataAdapter oledbda;

DataSet ds;

string str;

DataTable dt;

public Form1()

{

InitializeComponent();

}



private void button1_Click(object sender, EventArgs e)

{

OleDbConnection con = new OleDbConnection(ConnectionString);

con.Open();

str =select * from student;

com = new OleDbCommand(str,con);

oledbda = new OleDbDataAdapter(com);

ds = new DataSet();

oledbda.Fill(ds,student);

dt = ds .Tables [student];

int i;

for(i = 0;我< = dt.Rows.Count - 1; i ++)

{

comboBox1.Items.Add(dt.Rows [i] .ItemArray [0]);

}

con.Close();

}



private void button2_Click(object sender,EventArgs e)

{

OleDbConnection con = new OleDbConnection(ConnectionString);

con.Open();

str =select * from学生;

com = new OleDbCommand(str,con);

oledbda = new OleDbDataAdapter(com);

ds = new DataSet( );

oledbda.Fill(ds,student);

comboBox2.DataSource = ds.Tables [student];

comboBox2.DisplayMember =sname;

con.Close();

}



private void button3_Click (对象发送者,EventArgs e)

{

OleDbConnection con = new OleDbConnection(ConnectionString);

con.Open();

str =select * from student;

com = new OleDbCommand(str,con);

OleDbDataReader reader = com.ExecuteReader();

while(reader.Read())

{

comboBox3.Items.Add(reader [smarks]);

}

reader.Close ();

con.Close();

}



private void Form1_Load(object sender,EventArgs e)

{

comboBox1.Text =Plz点击按钮;

comboBox2.Text =Plz点击按钮;

comboBox3.Text =Plz点击按钮;

}

}

}
these are three types of methods to Bind the combo box

try this out..


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Dynamically_bind_data_to_combobox_from_database
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
DataSet ds;
string str;
DataTable dt;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from student";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "student");
dt = ds.Tables["student"];
int i;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
comboBox1.Items.Add(dt.Rows[i].ItemArray[0]);
}
con.Close();
}

private void button2_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from student";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "student");
comboBox2.DataSource = ds.Tables["student"];
comboBox2.DisplayMember = "sname";
con.Close();
}

private void button3_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from student";
com = new OleDbCommand(str, con);
OleDbDataReader reader = com.ExecuteReader();
while (reader.Read())
{
comboBox3.Items.Add(reader["smarks"]);
}
reader.Close();
con.Close();
}

private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Text="Plz Click the button";
comboBox2.Text="Plz Click the button";
comboBox3.Text="Plz Click the button";
}
}
}


这篇关于从sql表填充datagridviewcomboboxcolum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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