我用gridView在后台动态生成列和数据
现在要控制列样式,不是行样式,每一列的设置背景图片,
效果如图:
后台代码如下:
private void BindPriceByID(int bid,int cid)
{
/****************************************************
* 向dataGridView中添加数据,
***************************************************/
DataTable dtsupplymessage = new DataTable();//建立个数据表Class
string SQLstr = " supplyMessage_Order = 0 and supplyMessage_Supply = " + bid;
//从数据库读取
//BindingSource bdsInfo = new BindingSource();
DataSet ds = new DataSet();
DataSet dss;
string sqlBusiness = "supplyFiled_business=" + cid;
Hashtable htfiled = new Hashtable();
//获取记录字段
foreach (DataRow dr in supplyFiled_bll.GetList(sqlBusiness).Tables[0].Rows)
{
//向哈希表中存入记录字段
htfiled.Add(dr["supplyFiled_filedName"].ToString(), dr["supplyFiled_ID"].ToString());
}
ds = supplyMessage_bll.GetList(SQLstr);//内容
//nMax = supplyInfo_bll.GetTotalCount(SQLstr);//总条数
if (!ds.Tables[0].Rows.Count.Equals(0))
{
//向表Class中添加列
// dtsupplymessage.Columns.Add(new DataColumn("主键ID", typeof(string)));
foreach (DictionaryEntry item in htfiled)
{
if (item.Value.ToString().Equals(ds.Tables[0].Rows[0]["supplyMessage_SupplyFiled"].ToString()))
{
dtsupplymessage.Columns.Add(new DataColumn(item.Key.ToString(), typeof(string)));
}
}
foreach (DictionaryEntry item in htfiled)
{
if (!item.Value.ToString().Equals(ds.Tables[0].Rows[0]["supplyMessage_SupplyFiled"].ToString()))
{
dtsupplymessage.Columns.Add(new DataColumn(item.Key.ToString(), typeof(string)));
}
}
//向表中添加行
DataRow dr;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dr = dtsupplymessage.NewRow();
foreach (DictionaryEntry item in htfiled)//显示order为0的首字段
{
if (item.Value.ToString().Equals(ds.Tables[0].Rows[i]["supplyMessage_SupplyFiled"].ToString()))
{
//dr["主键ID"] = ds.Tables[0].Rows[i]["supplyMessage_ID"].ToString();
dr[item.Key.ToString()] = ds.Tables[0].Rows[i]["supplyMessage_Content"].ToString();
}
}
foreach (DictionaryEntry item in htfiled)//显示order为0的字段相关字段信息
{
dss = new DataSet();//子信息
string sql = " supplyMessage_Order = " + ds.Tables[0].Rows[i]["supplyMessage_ID"].ToString();
dss = supplyMessage_bll.GetList(sql);
for (int k = 0; k < dss.Tables[0].Rows.Count; k++)
{
if (item.Value.ToString().Equals(dss.Tables[0].Rows[k]["supplyMessage_SupplyFiled"].ToString()))
{
dr[item.Key.ToString()] = dss.Tables[0].Rows[k]["supplyMessage_Content"].ToString();
}
}
}
//向表Class中添加行
dtsupplymessage.Rows.Add(dr);
}
}
gvPrice.DataSource = dtsupplymessage;
gvPrice.DataBind();
}