将值从数据表绑定到Dropdownlist [英] Binding the values to Dropdownlist from datatable
问题描述
我有一个数据表,其中包含三行和三个颜色的数据。但是我想从datable填充第一行第二列coloumn数据到下拉列表。我不想填充数据表中的所有数据。你能帮忙吗?
你好b $ b首先,你必须选择你的DBMS类型(Access或oracle或sql server)
其次,你必须分三步工作:
1)从你数据库中的表中选择数据
2)将你选择的数据放入一个列表中(List< string> object)
3)现在你可以用列表中的项填充[DropdownList]< string>
例如,我使用SQL服务器作为DBMS,我在我的数据库中有一个[Table]命名程序包含两列[Id,Name]:
1)首先,我将填写List< string>使用静态方法:
静态 public List< string> SelectPrograms(List< string> program)
{
string ConnectionString = ConfigurationManager.ConnectionStrings [ ConnectionString]。ConnectionString;
SqlConnection connect = new SqlConnection(ConnectionString);
string query = 从程序中选择program_name ;跨度>;
SqlCommand command = new SqlCommand(query,connect);
connect.Open();
SqlDataReader reader = command.ExecuteReader();
尝试
{
string x;
while (reader.Read())
{
x = Convert.ToString(reader [ 0 跨度>]);
programs.Add(x);
}
}
最后
{
reader.Close();
connect.Close();
}
返回程序;
}
2)现在我将在使用List< string>之后使用最后一个方法变量
List< string> program = new List< string>();
program = [Put_ClassName] .SelectPrograms(program);
3)最后,我会填写我的[DropdownList]
for ( int i = 0 ; i& lt; program.Count; i ++)
{
DropDownList_ProgramName.Items.Add(方案[1]);
}
好锁......
from < a href =http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/805dcdb1-9e5c-4ac8-8a95-27868cdfe692> MSDN论坛 [ ^ ]
public partial class Form1:Form
{
DataSet ds;
public Form1()
{
InitializeComponent();
ds = new DataSet();
// 我将手动创建并填充dataTable,
// 您将使用sqlDataAdapter填充()方法:
< span class =code-comment> // 所以这不适合你
DataTable table = new DataTable( MyTable);
table.Columns.Add( id, typeof ( int ));
table.Columns.Add( name, typeof ( string ));
string [] names = { A, B, C, A , B};
DataRow博士;
for ( int i = 0 ; i < names.Length; i ++)
{
dr = table.NewRow();
dr [ id] = i + 1 跨度>;
dr [ name] = names [i];
table.Rows.Add(dr);
}
// 到此为止!
// 这是:
ds.Tables.Add(table) ;
dropdownlistid.DataSource = ds.Tables [ MyTable];
dropdownlistid.DisplayMember = name;
dropdownlistid.ValueMember = id;
}
}
或者试试吧:
http://shahed-kazi.blogspot.in/2009/03/bind-datatable-to-dropdownlist.html [ ^ ]
I have a datatable which contains the data like three rows and three coloumns.But I would like to populate the only first row second coloumn data to the dropdownlist from datable.I dont want to populate all the data which is in datatable.Can you help?
Hi Firstly, you must choose what is your DBMS type (Access or oracle or sql server)
Secondly, you must work in three steps:
1)Select your data from the table in your database
2)put your selected data in a list (List<string> object)
3)Now you can fill the [DropdownList] by items in your List<string>
As example,I used SQL server as DBMS and I have in my database a [Table] named program contains two columns [Id,Name] :
1) Firstly,I''ll fill the List<string> by using static Method :
static public List<string> SelectPrograms(List<string> programs) { string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection connect = new SqlConnection(ConnectionString); string query = "select program_name from Program;"; SqlCommand command = new SqlCommand(query, connect); connect.Open(); SqlDataReader reader = command.ExecuteReader(); try { string x; while (reader.Read()) { x = Convert.ToString(reader[0]); programs.Add(x); } } finally { reader.Close(); connect.Close(); } return programs; }
2)Now I''ll use the last method after I take a List<string> variable
List<string> program = new List<string>(); program = [Put_ClassName].SelectPrograms(program);
3)Finally, I''ll Fill my [DropdownList]
for (int i = 0; i < program.Count; i++) { DropDownList_ProgramName.Items.Add(program[i]); }
Good lock....
from MSDN Forum[^]
public partial class Form1 : Form { DataSet ds; public Form1() { InitializeComponent(); ds = new DataSet(); //I will create and populate dataTable manually, //you will do it with sqlDataAdapter Fill() method: //so this is not for you DataTable table = new DataTable("MyTable"); table.Columns.Add("id", typeof(int)); table.Columns.Add("name", typeof(string)); string[] names = { "A", "B", "C", "A", "B" }; DataRow dr; for (int i = 0; i < names.Length; i++) { dr = table.NewRow(); dr["id"] = i + 1; dr["name"] = names[i]; table.Rows.Add(dr); } //up to here! //this is: ds.Tables.Add(table); dropdownlistid.DataSource = ds.Tables["MyTable"]; dropdownlistid.DisplayMember = "name"; dropdownlistid.ValueMember = "id"; } }
or else try it :
http://shahed-kazi.blogspot.in/2009/03/bind-datatable-to-dropdownlist.html[^]
这篇关于将值从数据表绑定到Dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!