winForm下改变DataGridView的某行某列的值和控制列的位置
控制DataGrieView的某列的位置可以这样做:
this.dataGridView1.Columns["OperType"].DisplayIndex=5;
则OperType列的位置就是在第6列上。DisplayIndex的索引是从0开始的。
如果改变DataGridView的某行某列的值则可以这样做:
private void dgv_serverLRRecord_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{//以下两行分别是设置列标头居中和单元格内数据居中
this.dgv_serverLRRecord.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dgv_serverLRRecord.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
int _count = this.dgv_serverLRRecord.Rows.Count;
//if (!this.dgv_serverLRRecord.Columns.Contains("LRType"))
//{
// this.dgv_serverLRRecord.Columns.Add("LRType", "借还车类型");
// this.dgv_serverLRRecord.Columns["LRType"].Width = 150;
// this.dgv_serverLRRecord.Columns["LRType"].DisplayIndex = 5;//控件列的位置
// this.dgv_serverLRRecord.Columns["LRType"].Visible = false;
//}
for (int i = 0; i < _count; i++)
{
string _tempVal = string.Empty;
if (this.dgv_serverLRRecord.Rows[i].Cells["OperType"].Value.ToString() == "1")
{
_tempVal = "借车";
}
else if (this.dgv_serverLRRecord.Rows[i].Cells["OperType"].Value.ToString() == "2")
{
_tempVal = "还车";
}
else
{
_tempVal = "异常记录";
}
this.dgv_serverLRRecord.Rows[i].Cells["OperType"].Value = _tempVal;
//this.dgv_serverLRRecord.Rows[i].Cells["LRType"].Value = _tempVal;
}
this.dgv_serverLRRecord.Columns["BikeSN"].HeaderText = "自行车编号";
this.dgv_serverLRRecord.Columns["RentalName"].HeaderText = "租凭点名称";
this.dgv_serverLRRecord.Columns["MachineSN"].HeaderText = "停车桩编号";
this.dgv_serverLRRecord.Columns["RentalSN"].HeaderText = "租赁点编号";
this.dgv_serverLRRecord.Columns["Premoney"].HeaderText = "交易前余额(元)";
this.dgv_serverLRRecord.Columns["Premoney"].Width = 113;
this.dgv_serverLRRecord.Columns["Money"].HeaderText = "交易金额(元)";
this.dgv_serverLRRecord.Columns["Money"].Width = 110;
this.dgv_serverLRRecord.Columns["OperType"].HeaderText = "借还车类型";
this.dgv_serverLRRecord.Columns["OperDate"].HeaderText = "借还车时间";
this.dgv_serverLRRecord.Columns["OperDate"].Width = 103;
this.dgv_serverLRRecord.Columns["Address"].HeaderText = "租赁点地址";
this.dgv_serverLRRecord.Columns["Address"].Width = 160;
this.dgv_serverLRRecord.Columns["AllMoney"].Visible = false;
this.dgv_serverLRRecord.Rows[0].Cells[0].Selected = false;
}
本博客内容,如需转载请务必保留超链接。Contact Me:Mail此处省略好几个字...