using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void xTRAFFIC_LOGBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.xTRAFFIC_LOGBindingSource.EndEdit();
this.xTRAFFIC_LOGTableAdapter.Update(this.eXECRPTDataSet.XTRAFFIC_LOG);
}
private void SetDatagridState(DataTable Tab)
{
DataGridTableStyle tabStyle = new DataGridTableStyle();
tabStyle.MappingName = Tab.TableName;
for (int i = 0; i < Tab.Columns.Count; i++)
{
if (i == 4 | i == 6)
{
DataGridComboColumnString rCell = new DataGridComboColumnString();
rCell.MappingName = Tab.Columns[i].ColumnName;
rCell.HeaderText = Tab.Columns[i].ColumnName;
tabStyle.GridColumnStyles.Add(rCell);
}
else
{
DataGridNoActiveCellColumn rCell = new DataGridNoActiveCellColumn();
rCell.MappingName = Tab.Columns[i].ColumnName;
rCell.HeaderText = Tab.Columns[i].ColumnName;
tabStyle.GridColumnStyles.Add(rCell);
}
}
dataGrid1.TableStyles.Add(tabStyle);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“eXECRPTDataSet.XTRAFFIC_LOG”中。您可以根据需要移动或移除它。
this.xTRAFFIC_LOGTableAdapter.Fill(this.eXECRPTDataSet.XTRAFFIC_LOG);
this.dataGrid1.DataSource = this.eXECRPTDataSet.XTRAFFIC_LOG;
SetDatagridState(this.eXECRPTDataSet.XTRAFFIC_LOG);
//this.listBox1.Items.Add("字段 " + " 类型 " + " 长度");
this.textBox1.Text = "字段 " + " 类型 " + " 长度" + "/r/n";
for (int i = 0; i < eXECRPTDataSet.XTRAFFIC_LOG.Columns.Count - 1; i++)
{
//this.listBox1.Items.Add( this.eXECRPTDataSet.XTRAFFIC_LOG.Columns[i].ColumnName.ToString() + " " + this.eXECRPTDataSet.XTRAFFIC_LOG.Columns[i].DataType.ToString()+ " " + this.eXECRPTDataSet.XTRAFFIC_LOG.Columns[i].MaxLength.ToString());
this.textBox1.Text = this.textBox1.Text + this.eXECRPTDataSet.XTRAFFIC_LOG.Columns[i].ColumnName.ToString() + " " + this.eXECRPTDataSet.XTRAFFIC_LOG.Columns[i].DataType.ToString() + " " + this.eXECRPTDataSet.XTRAFFIC_LOG.Columns[i].MaxLength.ToString() + "/r/n";
}
this.dataGrid1.ReadOnly = true;
}
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
/*
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if (hti.Type == DataGrid.HitTestType.Cell && e.Button == MouseButtons.Left)
{
dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
dataGrid1.Select(hti.Row);
}
*/
}
private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
/*
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if (e.Button == MouseButtons.Left && e.Clicks == 2 && hti.Type == DataGrid.HitTestType.Cell)
{
MessageBox.Show("DoubleClick");
}
*/
}
}
#region 重载DataGridTextBoxColumn类的Edit方法,以便使点击DataGrid中任一单元格都选中当前行
internal class DataGridNoActiveCellColumn : System.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;
if (rowNum != 0 && this.DataGridTableStyle.DataGrid.IsSelected(rowNum - 1) == true)
{
this.DataGridTableStyle.DataGrid.UnSelect(rowNum - 1);
}
//this.DataGridTableStyle.DataGrid.row();
this.DataGridTableStyle.DataGrid.Select(SelectedRow);
}
}
#endregion
#region 本类实现DataGrid中实现ComboBox样式,并取得选取的字符串的值
///<summary>
/// ·本类实现DataGrid中实现ComboBox样式,并取得选取的字符串的值
///</summary>
public class DataGridComboColumnString : DataGridTextBoxColumn
{
private ComboBox comboBox;
private CurrencyManager _source; //当前管理的数据源
private int rowNum; //记录显示控件的行
public ComboBox Cb
{
get
{
return comboBox;
}
}
public DataGridComboColumnString()
{
comboBox = new ComboBox();
this.comboBox.Leave += new EventHandler(comboBox_leave);
this.comboBox.SelectedValueChanged += new EventHandler(Select_click);
}
//comboBox 焦点丢失时,控件自动隐藏
private void comboBox_leave(object sender, EventArgs e)
{
comboBox.Visible = false;
}
//当选取下拉列表中的字符串时,将获取的字符串值返回到DataGrid中去
private void Select_click(object sender, EventArgs e)
{
this.SetColumnValueAtRow(_source, rowNum, comboBox.SelectedItem.ToString());//设置当前列中选中行的字符串值
comboBox.Visible = false;
}
//重写Edit方法,以将Text控件的显示样式改为comboBox显示样式
protected override void Edit(CurrencyManager dataSource, int rowNum, Rectangle bounds, bool readOnly, string instanttext, bool cellVisible)
{
base.Edit(dataSource, rowNum, bounds, readOnly, instanttext, cellVisible);
comboBox.Parent = this.TextBox.Parent;
comboBox.Left = this.TextBox.Left - 2;
comboBox.Top = this.TextBox.Top - 2;
comboBox.Size = new Size(this.TextBox.Width, this.comboBox.Height);
this.TextBox.Visible = false;
comboBox.Visible = true;
comboBox.BringToFront();
comboBox.Focus();
_source = dataSource;
this.rowNum = rowNum;
}
}
#endregion
}