private void inidataGrid(DataView dv,string mappingName)
{
dv.AllowNew=false;
dv.AllowDelete=false;
this.dG1.TableStyles.Clear();
this.dG1.DataSource=dv;
DataGridTableStyle ts= new DataGridTableStyle();
ts.MappingName =mappingName;
ts.AlternatingBackColor=System.Drawing .Color.Bisque;
DataGridTextBoxColumn TxtCol =new DataGridTextBoxColumn();
TxtCol.MappingName ="name";
TxtCol.HeaderText ="名称";
TxtCol.Width =150;
TxtCol.NullText="";
TxtCol.ReadOnly=true;
TxtCol.TextBox.TabStop=true;
ts.GridColumnStyles.Add(TxtCol); TxtCol=new DataGridTextBoxColumn();
TxtCol.MappingName ="specify";
TxtCol.HeaderText ="规格";
TxtCol.Width =150;
TxtCol.NullText="";
TxtCol.ReadOnly=true;
TxtCol.TextBox.TabStop=true;
ts.GridColumnStyles.Add(TxtCol); TxtCol=new DataGridTextBoxColumn();
TxtCol.MappingName ="yearnumber";
TxtCol.HeaderText ="年限";
TxtCol.Width =150;
TxtCol.NullText="";
TxtCol.ReadOnly=true;
TxtCol.TextBox.TabStop=true;
ts.GridColumnStyles.Add(TxtCol);
TxtCol=new DataGridTextBoxColumn();
TxtCol.MappingName ="sumorwin";
TxtCol.HeaderText ="分类";
TxtCol.Width =150;
TxtCol.NullText="";
TxtCol.ReadOnly=true;
TxtCol.TextBox.TabStop=true;
ts.GridColumnStyles.Add(TxtCol);
dG1.AllowNavigation=false;
dG1.TableStyles.Add(ts);
}
{
dv.AllowNew=false;
dv.AllowDelete=false;
this.dG1.TableStyles.Clear();
this.dG1.DataSource=dv;
DataGridTableStyle ts= new DataGridTableStyle();
ts.MappingName =mappingName;
ts.AlternatingBackColor=System.Drawing .Color.Bisque;
DataGridTextBoxColumn TxtCol =new DataGridTextBoxColumn();
TxtCol.MappingName ="name";
TxtCol.HeaderText ="名称";
TxtCol.Width =150;
TxtCol.NullText="";
TxtCol.ReadOnly=true;
TxtCol.TextBox.TabStop=true;
ts.GridColumnStyles.Add(TxtCol); TxtCol=new DataGridTextBoxColumn();
TxtCol.MappingName ="specify";
TxtCol.HeaderText ="规格";
TxtCol.Width =150;
TxtCol.NullText="";
TxtCol.ReadOnly=true;
TxtCol.TextBox.TabStop=true;
ts.GridColumnStyles.Add(TxtCol); TxtCol=new DataGridTextBoxColumn();
TxtCol.MappingName ="yearnumber";
TxtCol.HeaderText ="年限";
TxtCol.Width =150;
TxtCol.NullText="";
TxtCol.ReadOnly=true;
TxtCol.TextBox.TabStop=true;
ts.GridColumnStyles.Add(TxtCol);
TxtCol=new DataGridTextBoxColumn();
TxtCol.MappingName ="sumorwin";
TxtCol.HeaderText ="分类";
TxtCol.Width =150;
TxtCol.NullText="";
TxtCol.ReadOnly=true;
TxtCol.TextBox.TabStop=true;
ts.GridColumnStyles.Add(TxtCol);
dG1.AllowNavigation=false;
dG1.TableStyles.Add(ts);
}
我再声明一下:是在winform下
固定列的意思是拖动水平滚动条的时候那些列不跟着移动。
外边框包住单元格的意思是就象Excel或着C1FlexGrid那样,点第一下的时候不是出现现在这种背景色是蓝色并且已经选中单元格中文本的状态,而是Cell的外边框被虚线罩住的样子。不知道我说清楚没有
using System.Drawing;
using System.Windows.Forms;
namespace test
{
/// <summary>
/// Class1 的摘要说明。
/// </summary> internal class DataGridNoActiveCellColumn : stem.Windows.Forms.DataGridTextBoxColumn
{
//重载DataGridTextBoxColumn类的Edit方法,以便使点击DataGrid中任一单元格都选中当前行
protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
int SelectedRow = rowNum;
this.DataGridTableStyle.DataGrid.Select(SelectedRow);
}
}
}
//最后定义 DataGridTextBoxColumn变量时换做 DataGridNoActiveCellColumn
//完毕
DataGridTableStyle ts = new DataGridTableStyle();
DataGridTextBoxColumn aColumnTextColumn = new DataGridTextBoxColumn();
DataGridTextBoxColumn.TextBox.ReadOnly;
ts.GridColumnStyles.Add(aColumnTextColumn);
this.datagrid.TableStyles.Add(ts);