DataGridView中一列一列的绑定(列名存在集合arrList中数据存在DataTable)
解释集合(arrList)中存SQL语句SELECT中的名字转换成DataTable的列名//通过集合中的名字创建列名
protected DataTable CreateTable(ArrayList arrList DataReader reader)
// reader可以从ExecuteReader获得
{
// DataTable的名字
DataTable dt = new DataTable(table);
//给DataTable定列名
for (int i = ; i < arrListCount; i++)
{
DataColumn dtColumn = new DataColumn();
dtColumnColumnName = (string)arrList[i];
dtColumnsAdd(dtColumn);
}
//往DataTable里加行
DataRow dtRow;
while (readerRead())
{
dtRow = dtNewRow();
for (int i = ; i < arrListCount; i++)
{
try
{
dtRow[(string)arrList[i]] = readerGetValue(i)ToString();
}
catch
{
dtRow[(string)arrList[i]] = SystemDBNullValue;
}
}
dtRowsAdd(dtRow);
}
return dt;
}
DataTable m_listViewInfo = CreateTable(集合名DataReader名)
//将DataTable绑定到DataView中
DataView dvListViewInfo = new DataView(m_listViewInfo);
dataGridViewColumnsClear();
//取消dataGridView按默认方式显示
dataGridViewAutoGenerateColumns = false;
//绑定DataView到dataGridView上
dataGridViewDataSource = dvListViewInfo;
DataGridViewTextBoxColumn dtcTimeStamp = new DataGridViewTextBoxColumn();
dtcTimeStampDataPropertyName = TIMESTAMP;//SQL语句得到的列名可从集合中获得dtcTimeStampHeaderText = 日期;//列头显示的汉字dtcTimeStampWidth = ;
dataGridViewColumnsAdd(dtcTimeStamp);//最后一定要添加进去