datagridview绑定列问题
this.Column3.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
this.Column3.DataPropertyName = "ID";
this.Column3.HeaderText = "编号";
this.Column3.MinimumWidth = 100;
this.Column3.Name = "Column3";
this.Column3.ReadOnly = true;
如我想在datagridview一个列里面绑定数据库ID,ID2两个字段,如何绑定?
[解决办法]
protected DataTable CreateTable(ArrayList arrList, DataReader reader)
// reader可以从ExecuteReader获得
{
// DataTable的名字
DataTable dt = new DataTable("table");
//给DataTable定列名
for (int i = 0; i < arrList.Count; i++)
{
DataColumn dtColumn = new DataColumn();
dtColumn.ColumnName = (string)arrList[i];
dt.Columns.Add(dtColumn);
}
//往DataTable里加行
DataRow dtRow;
while (reader.Read())
{
dtRow = dt.NewRow();
for (int i = 0; i < arrList.Count; i++)
{
try
{
dtRow[(string)arrList[i]] = reader.GetValue(i).ToString();
}
catch
{
dtRow[(string)arrList[i]] = System.DBNull.Value;
}
}
dt.Rows.Add(dtRow);
}
return dt;
}