Author: Haiya
1. Obtain the value of row i and column j of the data table
// Establish and open the database connection
OleDbConnection conn=new OleDbConnection();
conn.ConnectionString=strConnectionString;//strConnectionString is the database connection string
conn.Open();
string sql="select * from NewsClass order by ClassId desc";
string x;
DataSet ds=new DataSet();
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
da.Fill(ds,"NewsTable");
DataTable dt=ds.Tables["NewsTable"];
x=dt.Rows[i][1].ToString()//The value of row i and column j of the data table
conn.close();
2. Read data into DropDownList
(1) Add data to DropDownList
// Establish and open a database connection
OleDbConnection conn=new OleDbConnection();
conn.ConnectionString=strConnectionString;//strConnectionString is the database connection string
conn.Open();
string sql="select * from NewsClass order by ClassId desc";
//Create data set
DataSet ds=new DataSet();
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
da.Fill(ds,"NewsTable");
this.DropDownList1.DataSource=ds;
this.DropDownList1.DataTextField = "ClassName";//Text value
this.DropDownList1.DataValueField = "ClassID";//Value value
this.DropDownList1.DataBind();
conn.Close();
(2) Select an item of DropDownList
this.DropDownList1.Items.FindByValue(dr["ClassID"].ToString().Trim()).Selected=true;//dr is DataRow
3. Classification The coding retrieves the corresponding category name and displays it in the DataGrid
(1). Code in ASPX (ClassID is the category encoding):
<asp:TemplateColumn HeaderText="Category">
<ItemTemplate>
<asp:Label id=lblClass runat="server" Text='<%# GetClassName(Convert.ToInt32(DataBinder.Eval(Container, "DataItem.ClassID"))) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
(2) C# code:
/// <summary>
/// The "category" column returns text based on numbers
/// </summary>
/// <param name="IsPassed"></param>
/// <returns></returns>
public string GetClassName(int ClassID)
{
OleDbConnection conn=new OleDbConnection();
conn.ConnectionString=strConnectionString;
conn.Open();
string sql="select * from NewsClass where ClassID="+ClassID;
DataSet ds=new DataSet();
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
da.Fill(ds,"ClassTable");
DataTable dt=ds.Tables["ClassTable"];
string strClassName=dt.Rows[0]["ClassName"].ToString();
conn.Close();
return strClassName;//Return the ClassName corresponding to ClassID
}