|
|
|
C# >>>ComboBox C# ดึงค่าจากฐานข้อมูลมาโชว์ใน comboBox ค่ะไว้ให้เราเลือกอ่ะค่ะ ขอคำแนะนำหน่อยนะค่ะ |
|
|
|
|
|
|
|
(C#) ASP.NET DropDownlist & DataBinding
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DropDownListDataTable();
DropDownListDataTableRows();
DropDownListSortedList();
DropDownListAddInsertItem();
}
}
//*** DropDownList & DataTable ***//
void DropDownListDataTable()
{
OleDbConnection objConn;
OleDbDataAdapter dtAdapter;
DataTable dt = new DataTable();
String strConnString;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+
Server.MapPath("database/mydatabase.mdb")+"";
objConn = new OleDbConnection(strConnString);
objConn.Open();
String strSQL;
strSQL = "SELECT * FROM country";
dtAdapter = new OleDbDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
dtAdapter = null;
objConn.Close();
objConn = null;
//*** DropDownList ***//
this.myDDL1.DataSource = dt;
this.myDDL1.DataTextField = "CountryName";
this.myDDL1.DataValueField = "CountryCode";
this.myDDL1.DataBind();
//*** Default Value ***//
myDDL1.SelectedIndex = myDDL1.Items.IndexOf(myDDL1.Items.FindByValue("TH")); //*** By DataValueField ***//
//myDDL1.SelectedIndex = myDDL1.Items.IndexOf(myDDL1.Items.FindByText("Thailand")); //*** By DataTextField ***//
}
//*** DropDownList & TableRows ***//
void DropDownListDataTableRows()
{
DataTable dt = new DataTable();
DataRow dr;
//*** Column ***//
dt.Columns.Add("Sex");
dt.Columns.Add("SexDesc");
//*** Rows ***//
dr = dt.NewRow();
dr["Sex"] = "M";
dr["SexDesc"] = "Man";
dt.Rows.Add(dr);
//*** Rows ***//
dr = dt.NewRow();
dr["Sex"] = "W";
dr["SexDesc"] = "Woman";
dt.Rows.Add(dr);
//*** DropDownList ***//
this.myDDL2.DataSource = dt;
this.myDDL2.DataTextField = "SexDesc";
this.myDDL2.DataValueField = "Sex";
this.myDDL2.DataBind();
//*** Default Value ***//
myDDL2.SelectedIndex = myDDL2.Items.IndexOf(myDDL2.Items.FindByValue("W")); //*** By DataValueField ***//
//myDDL2.SelectedIndex = myDDL2.Items.IndexOf(myDDL2.Items.FindByText("Woman")); //*** By DataTextField ***//
}
//*** DropDownList & SortedList ***//
void DropDownListSortedList()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** DropDownList ***//
this.myDDL3.DataSource = mySortedList;
this.myDDL3.DataTextField = "Value";
this.myDDL3.DataValueField = "Key";
this.myDDL3.DataBind();
//*** Default Value ***//
myDDL3.SelectedIndex = myDDL3.Items.IndexOf(myDDL3.Items.FindByValue("W")); //*** By DataValueField ***//
//myDDL3.SelectedIndex = myDDL3.Items.IndexOf(myDDL3.Items.FindByText("Woman")); //*** By DataTextField ***//
}
//*** Add/Insert Items ***//
void DropDownListAddInsertItem()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("M","Man");
mySortedList.Add("W","Woman");
//*** DropDownList ***//
this.myDDL4.DataSource = mySortedList;
this.myDDL4.DataTextField = "Value";
this.myDDL4.DataValueField = "Key";
this.myDDL4.DataBind();
//*** Add & Insert New Item ***//
String strText,strValue;
//*** Insert Item ***//
strText = "";
strValue = "";
ListItem InsertItem = new ListItem(strText, strValue);
myDDL4.Items.Insert(0, InsertItem);
//*** Add Items ***//
strText = "Guy";
strValue = "G";
ListItem AddItem = new ListItem(strText, strValue);
myDDL4.Items.Add(AddItem);
//*** Default Value ***//
myDDL4.SelectedIndex = myDDL4.Items.IndexOf(myDDL4.Items.FindByValue("")); //*** By DataValueField ***//
//myDDL4.SelectedIndex = myDDL4.Items.IndexOf(myDDL4.Items.FindByText("")); //*** By DataTextField ***//
}
void Button1_OnClick(object sender, EventArgs e)
{
this.lblText1.Text = this.myDDL1.SelectedItem.Value; //*** Or this.myDDL1.SelectedItem.Text ***//
this.lblText2.Text = this.myDDL2.SelectedItem.Value; //*** Or this.myDDL2.SelectedItem.Text ***//
this.lblText3.Text = this.myDDL3.SelectedItem.Value; //*** Or this.myDDL3.SelectedItem.Text ***//
this.lblText4.Text = this.myDDL4.SelectedItem.Value; //*** Or this.myDDL4.SelectedItem.Text ***//
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - DropDownList & DataBind</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList id="myDDL1" runat="server"></asp:DropDownList>
<asp:DropDownList id="myDDL2" runat="server"></asp:DropDownList>
<asp:DropDownList id="myDDL3" runat="server"></asp:DropDownList>
<asp:DropDownList id="myDDL4" runat="server"></asp:DropDownList>
<asp:Button id="Button1" onclick="Button1_OnClick" runat="server" Text="Button"></asp:Button>
<hr />
<asp:Label id="lblText1" runat="server"></asp:Label><br />
<asp:Label id="lblText2" runat="server"></asp:Label><br />
<asp:Label id="lblText3" runat="server"></asp:Label><br />
<asp:Label id="lblText4" runat="server"></asp:Label><br />
</form>
</body>
</html>
|
|
|
|
|
Date :
2010-06-23 22:09:19 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# เป็น Windows app อ่ะค่ะ
|
|
|
|
|
Date :
2010-06-24 15:03:37 |
By :
oomi2830 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใช้ .DataSource เหมือนกันครับ
|
|
|
|
|
Date :
2010-06-24 22:47:39 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณค่ะ ได้แล้วค่ะ ^^
|
|
|
|
|
Date :
2010-06-25 16:52:18 |
By :
oomi2830 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
พี่ครับ ใช้โค้ด ตัวนี้ ผมสามารถ ดึงได้ แค่Combobox เดียว ถ้าผมต้องการ ดึก สองสาม Combobox ละครับคนละตารางอีกด้วย
ช่วยแนะนำหน่อยครับ..
Code (C#)
// การ ไปดึงข้อมูลจากตาราง Organization เพื่อที่จะมาโชร์ใน ComboBox
DataSet ds = new DataSet();
SqlDataAdapter da1 = new SqlDataAdapter("select * from Document", conn);
da1.Fill(ds, "combolist");
cmbDocID.DataSource = ds;//กำหนด DataSet
cmbDocID.DisplayMember = "combolist.DocName";
cmbDocID.ValueMember = "combolist.DocID";
// cmbCustomer.DisplayMember = "combolist.Name";
|
|
|
|
|
Date :
2013-06-17 11:50:12 |
By :
ธีระวุฒิ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ต้องการดึงข้อมูลที่ยังไม่ได้คืนมาโชว์ในcombobox ของอุปกรณ์สูญหายจะทำยังไงค่ะ รบกวนด้วยค่ะ
|
|
|
|
|
Date :
2013-11-14 14:58:49 |
By :
ด้อยปัญญา |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|