datagrid的问题

istring 2006-04-03 06:34:13
我记得可以在datagrid里实现这样的规则,
例如当值符合要求(例如大于0),则文字颜色改变,单击则触发事件,有谁记得如何做啊?
...全文
170 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zoxn 2006-04-05
  • 打赏
  • 举报
回复
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
dataGrid1.DataSource = SomeDataTable();
AddCellFormattingColumnStyles(this.dataGrid1, new FormatCellEventHandler(FormatGridCells));

}

private void FormatGridCells(object sender, DataGridFormatCellEventArgs e)
{
//color row 1 red
if(e.Row == 1)
e.BackBrush = Brushes.Red;

//color column 4 blue
if(e.Column == 1)
{
e.ForeBrush = Brushes.White;
e.BackBrush = Brushes.Blue;
}
//set font of some cells to bold
if( (e.Row + e.Column) % 5 == 0 )
e.TextFont = new Font(e.TextFont.Name, e.TextFont.Size, FontStyle.Bold);

//set textcolor of some cells to blue
if( (e.Row + e.Column) % 8 == 0 )
e.ForeBrush = Brushes.DodgerBlue;

//set font of some cells to bold, underline, italic with white text on green background
if( (e.Row + e.Column) % 9 == 0 )
{
e.TextFont = new Font(e.TextFont.Name, e.TextFont.Size, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
e.ForeBrush = Brushes.White;
e.BackBrush = Brushes.Green;
}


}

private DataTable SomeDataTable()
{
string SQLConn="server=(local);database=StudentDB;user id=sa;password=19840826";
System.Data.SqlClient.SqlConnection conn=new System.Data.SqlClient.SqlConnection(SQLConn);
string str="select * from user1";

System.Data.SqlClient.SqlDataAdapter sqlAdapter=new SqlDataAdapter(str,conn);
System.Data.DataSet st=new DataSet() ;
sqlAdapter.Fill(st,"table1");
DataTable dt=st.Tables["table1"];
return dt;
}

private void AddCellFormattingColumnStyles(DataGrid grid, FormatCellEventHandler handler)
{
DataGridTableStyle ts = new DataGridTableStyle();

DataTable dt = (DataTable) grid.DataSource;

ts.MappingName = dt.TableName;

for(int j = 0; j < dt.Columns.Count; ++j)
{
DataGridFormattableTextBoxColumn cs = new DataGridFormattableTextBoxColumn(j);
cs.MappingName = dt.Columns[j].ColumnName;
cs.HeaderText = dt.Columns[j].ColumnName;
cs.SetCellFormat += handler;
ts.GridColumnStyles.Add(cs);
}

grid.TableStyles.Clear();
grid.TableStyles.Add(ts);
}
}

public delegate void FormatCellEventHandler(object sender, DataGridFormatCellEventArgs e);

public class DataGridFormatCellEventArgs : EventArgs
{
private int _column;
private int _row;
private Font _font;
private Brush _backBrush;
private Brush _foreBrush;
private bool _useBaseClassDrawing;


public DataGridFormatCellEventArgs(int row, int col, Font font1, Brush backBrush, Brush foreBrush)
{
_row = row;
_column = col;
_font = font1;
_backBrush = backBrush;
_foreBrush = foreBrush;
_useBaseClassDrawing = false;
}

public int Column
{
get{ return _column;}
set{ _column = value;}
}
public int Row
{
get{ return _row;}
set{ _row = value;}
}
public Font TextFont
{
get{ return _font;}
set{ _font = value;}
}

public Brush BackBrush
{
get{ return _backBrush;}
set{ _backBrush = value;}
}
public Brush ForeBrush
{
get{ return _foreBrush;}
set{ _foreBrush = value;}
}
public bool UseBaseClassDrawing
{
get{ return _useBaseClassDrawing;}
set{ _useBaseClassDrawing = value;}
}
}

public class DataGridFormattableTextBoxColumn : DataGridTextBoxColumn
{
//in your handler, set the EnableValue to true or false, depending upon the row & col
public event FormatCellEventHandler SetCellFormat;

private int _col;

public DataGridFormattableTextBoxColumn(int col)
{
_col = col;
}
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
{
DataGridFormatCellEventArgs e = new DataGridFormatCellEventArgs(rowNum, this._col, this.DataGridTableStyle.DataGrid.Font, backBrush, foreBrush);
if(SetCellFormat != null)
{
SetCellFormat(this, e);
}
if(e.UseBaseClassDrawing)
{
base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
else
{
g.FillRectangle(e.BackBrush, bounds);
g.DrawString(this.GetColumnValueAtRow(source, rowNum).ToString(), e.TextFont, e.ForeBrush, bounds.X, bounds.Y);
}
if(e.TextFont != this.DataGridTableStyle.DataGrid.Font)
e.TextFont.Dispose();
}

protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
//comment to make cells unable to become editable
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
}
}
}
Lcindep110 2006-04-04
  • 打赏
  • 举报
回复
可以在ItemDataBound事件中写
if(e.Items.ItemsType == ListItemType.Item || e.Items.ItemsType == ListItemType.AlternatingItem)
{
//当DataGrid中,单元格第0个的值大于0时,让该行变为红色
int cel1 = Convert.ToInt32(e.Items.Cell[0].Text);
if(cel > 0)
{
e.Items.ForeColor = Color.Red;
}
}
istring 2006-04-04
  • 打赏
  • 举报
回复
怎么没人顶啊?????????巨失望—_-
jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻松的打造出功能丰富并且美观的UI界面。开发者不需要编写复杂的javascript,也不需要对css样式有深入的了解,开发者需要了解的只有一些简单的html标签 1.3.6更新 Bug treegrid: getChecked方法不能返回正确的行. fixed. tree: 异步树,在onlyLeafCheck:true时复选框不显示正确. fixed. Improvement treegrid:继承datagrid组件所有的selecting和checking方法。 linkbutton:图标对齐方式,支持值:'top','bottom','left','right'。 linkbutton:添加"size"属性,支持值:'small','large'。 linkbutton:添加的onClick事件。 menubutton:添加"menuAlign"属性,允许用户设置顶级菜单对齐。 combo:添加"panelAlign"属性,支持值:'left','right'。 calendar:"formatter"、"styler"和"validator"选项可用于自定义日历日期。 calendar:添加的onChange事件。 panel:添加"method","queryParams"和"loader"属性。 panel:添加"onLoadError"事件。 datagrid:添加"onBeginEdit"事件。 datagrid:添加"onEndEdit"事件。 datagrid:添加"sort"方法和"onBeforeSortColumn"事件。 datagrid:"combogrid"编辑器集成到datagriddatagrid:添加"ctrlSelect"属性,允许使用ctrl+click 多选 slider:添加"converter"选项,允许用户决定如何将一个值转换为滑块的位置或滑块位置值。 searchbox:添加"disabled"属性。 searchbox:添加"disabled","enable","clear","reset"方法。 spinner:添加"readonly"属性、"readonly"方法和"onChange事件。

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧