看上去没有输入焦点的只读DataGrid (WinForm)
假设已经在WinForm窗体上放置了一个名称为dataGridMsg的DataGrid,并且为窗体增加一个成员
private DataSet DataSetMsg;
在form的Load中添加以下代码
DataSetMsg = new DataSet("DataSetMsg");
DataTable dt= DataSetMsg.Tables.Add("DataTableMsg");
dt.Columns.Add("Code",typeof(string));
dt.Columns.Add("Msg",typeof(string));
dt.Columns.Add("V",typeof(string));//添加用于隐藏输入焦点的列
//添加10行
for(int B=0;B<10,B++)
{
DataRow dr = new dt.NewRow();
dr["Code"] = B.ToString().PadLeft(4,'0');
dr["Msg"] = "Msg"+B.ToString();
dr["V"] = "";
}
dataGridMsg.ReadOnly = true;
//指定DataGridMsg列的外观
DataView dataviewMsg = new DataView(DataSetMsg.Tables["DataTableMsg"]);
dataviewMsg.AllowDelete = false;
dataviewMsg.AllowNew = false;
dataGridMsg.SetDataBinding(dataviewMsg,"");
DataGridTableStyle ts1 = new DataGridTableStyle(false);
ts1.MappingName = "DataTableMsg";
ts1.AllowSorting = true;
ts1.GridColumnStyles.Clear();
//第0列--代码
DataGridTextBoxColumn CCode = new DataGridTextBoxColumn();
CCode.MappingName = "Code";
CCode.HeaderText = "代码";
CCode.Width = 100;
CCode.Alignment = HorizontalAlignment.Center;
CCode.ReadOnly = true;
ts1.GridColumnStyles.Add(CCode);
//第1列-- 信息
DataGridTextBoxColumn Cmsg = new DataGridTextBoxColumn();
Cmsg.MappingName = "Msg";
Cmsg.HeaderText = "信息";
Cmsg.ReadOnly = true;
Cmsg.Alignment = HorizontalAlignment.Left;
Cmsg.Width = 600;
ts1.GridColumnStyles.Add(Cmsg);
//第2列--用于隐藏输入焦点的列
DataGridTextBoxColumn CV = new DataGridTextBoxColumn();
CV.MappingName = "V";
CV.ReadOnly = true;
CV.Width = 0;//隐藏的列
ts1.GridColumnStyles.Add(CV);
dataGridMsg.TableStyles.Clear();
dataGridMsg.TableStyles.Add(ts1);
在 dataGridMsg的CurrentCellChanged 事件中添加以下代码
//将当前列始终设置为定义的隐藏列2
dataGridMsg.CurrentCell = new DataGridCell(dataGridMsg.CurrentCell.RowNumber,2);
private DataSet DataSetMsg;
在form的Load中添加以下代码
DataSetMsg = new DataSet("DataSetMsg");
DataTable dt= DataSetMsg.Tables.Add("DataTableMsg");
dt.Columns.Add("Code",typeof(string));
dt.Columns.Add("Msg",typeof(string));
dt.Columns.Add("V",typeof(string));//添加用于隐藏输入焦点的列
//添加10行
for(int B=0;B<10,B++)
{
DataRow dr = new dt.NewRow();
dr["Code"] = B.ToString().PadLeft(4,'0');
dr["Msg"] = "Msg"+B.ToString();
dr["V"] = "";
}
dataGridMsg.ReadOnly = true;
//指定DataGridMsg列的外观
DataView dataviewMsg = new DataView(DataSetMsg.Tables["DataTableMsg"]);
dataviewMsg.AllowDelete = false;
dataviewMsg.AllowNew = false;
dataGridMsg.SetDataBinding(dataviewMsg,"");
DataGridTableStyle ts1 = new DataGridTableStyle(false);
ts1.MappingName = "DataTableMsg";
ts1.AllowSorting = true;
ts1.GridColumnStyles.Clear();
//第0列--代码
DataGridTextBoxColumn CCode = new DataGridTextBoxColumn();
CCode.MappingName = "Code";
CCode.HeaderText = "代码";
CCode.Width = 100;
CCode.Alignment = HorizontalAlignment.Center;
CCode.ReadOnly = true;
ts1.GridColumnStyles.Add(CCode);
//第1列-- 信息
DataGridTextBoxColumn Cmsg = new DataGridTextBoxColumn();
Cmsg.MappingName = "Msg";
Cmsg.HeaderText = "信息";
Cmsg.ReadOnly = true;
Cmsg.Alignment = HorizontalAlignment.Left;
Cmsg.Width = 600;
ts1.GridColumnStyles.Add(Cmsg);
//第2列--用于隐藏输入焦点的列
DataGridTextBoxColumn CV = new DataGridTextBoxColumn();
CV.MappingName = "V";
CV.ReadOnly = true;
CV.Width = 0;//隐藏的列
ts1.GridColumnStyles.Add(CV);
dataGridMsg.TableStyles.Clear();
dataGridMsg.TableStyles.Add(ts1);
在 dataGridMsg的CurrentCellChanged 事件中添加以下代码
//将当前列始终设置为定义的隐藏列2
dataGridMsg.CurrentCell = new DataGridCell(dataGridMsg.CurrentCell.RowNumber,2);