给你一个关于DataGrid的代码,这是一个组件,全代码如下,你可以直接在工具箱中添加使用.加分哟.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;namespace My_Controls
{
/// <summary>
/// ListDataGrid 的摘要说明。
/// </summary>
///  public class ListDataGrid:System.Windows.Forms.DataGrid
{
public ListDataGrid()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void SetListData(ref DataSet ds)
{
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName =ds.Tables[0].TableName; int numCols = ds.Tables[0].Columns.Count;
DataGridNoActiveCellColumn textColumn;
for(int i = 0; i < numCols-1; ++i)
{
textColumn = new DataGridNoActiveCellColumn();
textColumn .HeaderText = ds.Tables[0].Columns[i].ColumnName; textColumn .MappingName = ds.Tables[0].Columns[i].ColumnName;
tableStyle.GridColumnStyles.Add(textColumn);
}
DataGridTextBoxColumn tc=new DataGridTextBoxColumn();
tc.HeaderText=ds.Tables[0].Columns[numCols-1].ColumnName;
tc.MappingName=ds.Tables[0].Columns[numCols-1].ColumnName;
tableStyle.GridColumnStyles.Add(tc);
this.TableStyles.Clear();

this.TableStyles.Add(tableStyle);
this.SetDataBinding(ds,ds.Tables[0].TableName);
CurrencyManager cm = (CurrencyManager)this.BindingContext[this.DataSource, this.DataMember];
((DataView)cm.List).AllowNew = false;
((DataView)cm.List).AllowDelete= true;
} private void InitializeComponent()
{
((System.ComponentModel.ISupportInitialize)(this)).BeginInit(); } private void ListDataGrid_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{
if(ne.Forward) 

CurrencyManager cm = (CurrencyManager)BindingContext[this.DataSource,this.DataMember]; 
DataView dv = (DataView) cm.List; 
dv.AllowNew = false; 

}
}
public class DataGridNoActiveCellColumn : DataGridTextBoxColumn
{
private int nSelectedRowIndex = -1;
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
if(nSelectedRowIndex> -1 && nSelectedRowIndex< source.List.Count + 1)
{
this.DataGridTableStyle.DataGrid.UnSelect(nSelectedRowIndex);
}
nSelectedRowIndex= rowNum;
this.DataGridTableStyle.DataGrid.Select(nSelectedRowIndex);
}
}
}