private string connstr = “Provider = Microsoft.Jet.OLEDB.4.0; Data Source = aaa.mdb”;
public DataSet getlist(string str, string tableName)
{
DataSet ds = new DataSet();
using (OleDbConnection conn = new OleDbConnection(connstr))
{
OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
da.Fill(ds, tableName);
}
//dataGridView1.DataSource = ds.Tables[tableName];
return ds;
}
string str = “select * from Person order by id asc”;
DataSet ds;
string tableName = “all”;
ds = getlist(str, tableName);
this.indexDataGridView1.DataSource = ds.Tables[tableName];
ds.Tables[tableName].Rows[2][8] = 5; //手动写入datagridview的值
绑定数据到combobox中
//要加上上面代码中的数据库连接和getlist方法
public void getClass()
{
string str = “select * from Class order by id asc”;
DataSet dsclass = new DataSet();
string tableName = “pclass”;
dsclass = getlist(str, tableName);
this.indexCbClassAdd.DataSource = dsclass.Tables[tableName]; //绑定DATASET到COMBOBOX
this.indexCbClassAdd.DisplayMember = “Class”; //显示成员内容
this.indexCbClassAdd.ValueMember = “ID”; //显示ID号
//以上部分就是绑定了combobox,下面是将dataset中的内容写入数组的方法
int tableCount = dsclass.Tables[tableName].Rows.Count; //获取DATASET内的行数
int[] arrPclassid = new int[tableCount]; //建立ID数组
for (int i = 0; i < tableCount; i++)
{
arrPclassid[i] = Convert.ToInt32(dsclass.Tables[tableName].Rows[i][“id”]); //循环ID的内容写入数组
}
string[] arrPcclass = new string[tableCount]; //建立CLASS数组
for (int i = 0; i < tableCount; i++)
{
arrPcclass[i] = Convert.ToString(dsclass.Tables[tableName].Rows[i][“Class”]); //循环CLASS的内容写入数组
}
//foreach (int arrclassid in arrPclassid) //遍历数组内容
//{
// MessageBox.Show(arrclassid.ToString);
//}
}
暂无评论内容