二//建立並打開數據庫連接
OleDbConnection conn=new OleDbConnection();
connConnectionString=strConnectionString;//strConnectionString為數據庫連接字符串
connOpen();
string sql=select * from NewsClass order by ClassId desc ;
string x;
DataSet ds=new DataSet();
OleDbDataAdapter da=new OleDbDataAdapter(sqlconn);
daFill(ds NewsTable );
DataTable dt=dsTables[ NewsTable ];
x=dtRows[i][ ] ToString()//數據表i行j列的值
connclose();
(//建立並打開數據庫連接
OleDbConnection conn=new OleDbConnection();
connConnectionString=strConnectionString;//strConnectionString為數據庫連接字符串
connOpen();
string sql=select * from NewsClass order by ClassId desc ;
//建立數據集
DataSet ds=new DataSet();
OleDbDataAdapter da=new OleDbDataAdapter(sqlconn);
daFill(ds NewsTable );
thisDropDownList DataSource=ds;
thisDropDownList DataTextField = ClassName ;//Text值
thisDropDownList DataValueField = ClassID ;//Value值
thisDropDownList DataBind();
connClose();
this
DropDownList Items FindByValue(dr[ ClassID ] ToString() Trim()) Selected=true; //dr為DataRow
三
(
<asp:TemplateColumn HeaderText=
類 別 >
<ItemTemplate>
<asp:Label id=lblClass runat=server Text=
<%# GetClassName(Convert ToInt (DataBinder Eval(Container
DataItem ClassID ))) %> >
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
(
///
///分類 列根據數字返回文字
///
///
///
public string GetClassName(int ClassID)
{
OleDbConnection conn=new OleDbConnection();
connConnectionString=strConnectionString;
connOpen();
string sql=select * from NewsClass where ClassID= +ClassID;
DataSet ds=new DataSet();
OleDbDataAdapter da=new OleDbDataAdapter(sqlconn);
daFill(ds ClassTable );
DataTable dt=dsTables[ ClassTable ];
string strClassName=dtRows[ ][ ClassName ] ToString();
connClose();
return strClassName;//返回 ClassID對應的ClassName
}
From:http://tw.wingwit.com/Article/program/net/201311/15348.html