1、绑定数据源
/***1DGrid**/#region /***1DGrid**/
/**//// <summary>
/// 查询数据库,提取试题数据
/// </summary>
private void addDataGridData()
{
try
{
AcceGetData data=new AcceGetData();
DataSet ds=data.GetEDataSet(p_mdbFullName,p_eTableName);
//this.dataGrid1.SetDataBinding(ds,"");
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember=ds.Tables[0].TableName;
//中文列名
DGStyle (ds.Tables[0]);
}
catch (Exception err)
{
MessageBox.Show(this,err.Message.ToString(),"提取1数据",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
#endregion
2、设置中文列名显示:/**//// <summary>
/// 查询数据库,提取试题数据
/// </summary>
private void addDataGridData()
{
try
{
AcceGetData data=new AcceGetData();
DataSet ds=data.GetEDataSet(p_mdbFullName,p_eTableName);
//this.dataGrid1.SetDataBinding(ds,"");
this.dataGrid1.DataSource=ds;
this.dataGrid1.DataMember=ds.Tables[0].TableName;
//中文列名
DGStyle (ds.Tables[0]);
}
catch (Exception err)
{
MessageBox.Show(this,err.Message.ToString(),"提取1数据",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
#endregion
/*11中文列名*/#region /*11中文列名*/
private void DGStyle (DataTable tab)
{
ArrayList myArray=new ArrayList(); //13
myArray.Add("序号");
myArray.Add("年份");
myArray.Add("地区");
……
myArray.Add("说明");
DataGridTableStyle tableStyle = new DataGridTableStyle();
if(dataGrid1.TableStyles.Count==0)
{
tableStyle.MappingName =tab.TableName; // 设定数据源的表名
tab.DefaultView.AllowNew= true;
tableStyle.RowHeadersVisible=true;
tableStyle.RowHeaderWidth=10;
//设定表格 表头,奇数列等的颜色 tableStyle.AlternatingBackColor=Color.Lavender;
tableStyle.HeaderBackColor=Color.LightSteelBlue;
tableStyle.SelectionBackColor=Color.CadetBlue;
for (int i=0;i<tab.Columns.Count;i++)
{
DataGridTextBoxColumn TxtCol = new DataGridTextBoxColumn();
TxtCol = new DataGridTextBoxColumn();
TxtCol.MappingName=tab.Columns[i].ColumnName ; //数据源的列名
TxtCol.HeaderText= myArray [i].ToString() ; //DataGrid上的列名
TxtCol.ReadOnly = false; //设置列属性的可修改性
//
TxtCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
//
//设置列宽
if(i==8||i==9)
{
TxtCol.Width=0;
}
else
{
TxtCol.Width=60;
}
TxtCol.NullText = "未设定"; //该单元数据在数据库中值为NULL时的显示文本
tableStyle.GridColumnStyles.Add(TxtCol); //添加到样式列表中
}
}
dataGrid1.TableStyles.Add (tableStyle);
}
private void TextBoxDoubleClickHandler(object sender, EventArgs e)
{
//记忆当前行号
currentERowIndex=this.dataGrid1.CurrentCell.RowNumber;
GetSomeFileName(dataGrid1,currentERowIndex);
dataGrid1.Select(currentERowIndex);
}
#endregion
private void DGStyle (DataTable tab)
{
ArrayList myArray=new ArrayList(); //13
myArray.Add("序号");
myArray.Add("年份");
myArray.Add("地区");
……
myArray.Add("说明");
DataGridTableStyle tableStyle = new DataGridTableStyle();
if(dataGrid1.TableStyles.Count==0)
{
tableStyle.MappingName =tab.TableName; // 设定数据源的表名
tab.DefaultView.AllowNew= true;
tableStyle.RowHeadersVisible=true;
tableStyle.RowHeaderWidth=10;
//设定表格 表头,奇数列等的颜色 tableStyle.AlternatingBackColor=Color.Lavender;
tableStyle.HeaderBackColor=Color.LightSteelBlue;
tableStyle.SelectionBackColor=Color.CadetBlue;
for (int i=0;i<tab.Columns.Count;i++)
{
DataGridTextBoxColumn TxtCol = new DataGridTextBoxColumn();
TxtCol = new DataGridTextBoxColumn();
TxtCol.MappingName=tab.Columns[i].ColumnName ; //数据源的列名
TxtCol.HeaderText= myArray [i].ToString() ; //DataGrid上的列名
TxtCol.ReadOnly = false; //设置列属性的可修改性
//
TxtCol.TextBox.DoubleClick += new EventHandler(TextBoxDoubleClickHandler);
//
//设置列宽
if(i==8||i==9)
{
TxtCol.Width=0;
}
else
{
TxtCol.Width=60;
}
TxtCol.NullText = "未设定"; //该单元数据在数据库中值为NULL时的显示文本
tableStyle.GridColumnStyles.Add(TxtCol); //添加到样式列表中
}
}
dataGrid1.TableStyles.Add (tableStyle);
}
private void TextBoxDoubleClickHandler(object sender, EventArgs e)
{
//记忆当前行号
currentERowIndex=this.dataGrid1.CurrentCell.RowNumber;
GetSomeFileName(dataGrid1,currentERowIndex);
dataGrid1.Select(currentERowIndex);
}
#endregion