DataGrid中bool类型的问题

xiaoyao128 2005-12-16 07:24:41
在SQL数据库中设置一bit类型的字段
会自动在DataGrid中显示为CheckBox的样子
可是会有选中,不选中,灰色三种状态
怎么设置其只有选中与不选中两种状态呢???

第二个问题
怎么实现datagrid中的类似级联修改的功能?
比如在DataGrid中第1列显示编码(0,1)
第2列显示该编码所对应的数值
当第一列的编码修改后,第二列的值相应发生变化
...全文
119 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyao128 2005-12-17
  • 打赏
  • 举报
回复
继续求助,上面的两个问题:


sqlDataAdapter1.Fill(dataSet11);

datagridtablestyle aa = new datagridtablestyle();
datagridboolcolumn bb = new datagridboolcolumn();
bb.allownull = false;
aa.gridcolumnstyles.add(bb);
datagrid1.tablestyles.add(aa);

dataGrid1.DataSource = dataSet11.Tables[0];


我用这样的代码为什么不能实现第一个问题呢?
wuzhijie 2005-12-17
  • 打赏
  • 举报
回复
第一个问题,灰表示值为null,设一个默认值(0,或1)就不会有灰的情况了
runrunrun 2005-12-16
  • 打赏
  • 举报
回复
响应Grid 的AfterEdit消息
xiaoyao128 2005-12-16
  • 打赏
  • 举报
回复
接上


public Color TrueColor
{
get
{
return trueBrush.Color;
}
set
{
trueBrush = new SolidBrush(value);
Invalidate();
}
}

public DataGridBoolColumnInherit() : base ()
{
count ++;
}


// This will work only with a DataSet or DataTable.
// The code is not compatible with IBindingList implementations.
public string Expression
{
get
{
return this.expressionColumn == null ? String.Empty :
this.expressionColumn.Expression;
}
set
{
if (expressionColumn == null)
AddExpressionColumn(value);
else
expressionColumn.Expression = value;
if (expressionColumn != null &&
expressionColumn.Expression.Equals(value))
return;
Invalidate();
}
}

private void AddExpressionColumn(string value)
{
// Get the grid's data source. First check for a null
// table or data grid.
if (this.DataGridTableStyle == null ||
this.DataGridTableStyle.DataGrid == null)
return;

DataGrid myGrid = this.DataGridTableStyle.DataGrid;
DataView myDataView = ((CurrencyManager)
myGrid.BindingContext[myGrid.DataSource,
myGrid.DataMember]).List
as DataView;

// This works only with System.Data.DataTable.
if (myDataView == null)
return;

// If the user already added a column with the name
// then exit. Otherwise, add the column and set the
// expression to the value passed to this function.
DataColumn col = myDataView.Table.Columns["__Computed__Column__"];
if (col != null)
return;
col = new DataColumn("__Computed__Column__" + count.ToString());

myDataView.Table.Columns.Add(col);
col.Expression = value;
expressionColumn = col;
}

// override the OnPaint method to paint the cell based on the expression.
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source, int rowNum,
Brush backBrush, Brush foreBrush,
bool alignToRight)
{
bool trueExpression = false;
bool hasExpression = false;
DataRowView drv = source.List[rowNum] as DataRowView;

hasExpression = this.expressionColumn != null &&
this.expressionColumn.Expression != null &&
!this.expressionColumn.Expression.Equals(String.Empty);

Console.WriteLine(string.Format("hasExpressionValue {0}",hasExpression));
// Get the value from the expression column.
// For simplicity, we assume a True/False value for the
// expression column.
if (hasExpression)
{
object expr = drv.Row[expressionColumn.ColumnName];
trueExpression = expr.Equals("True");
}

// Let the DataGridBoolColumn do the painting.
if (!hasExpression)
base.Paint(g, bounds, source, rowNum,
backBrush, foreBrush, alignToRight);

// Paint using the expression color for true or false, as calculated.
if (trueExpression)
base.Paint(g, bounds, source, rowNum,
trueBrush, foreBrush, alignToRight);
else
base.Paint(g, bounds, source, rowNum,
falseBrush, foreBrush, alignToRight);
}
}
xiaoyao128 2005-12-16
  • 打赏
  • 举报
回复
我的是winForm的
刚才查了半天的MSDN ,有点收获
自己解决了第一个问题,贴出来给大家
这是一个MSDN中的例子:

using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

public class MyForm : Form
{
private System.Windows.Forms.DataGrid myGrid;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button button1;
private DataTable myTable;

public MyForm() : base()
{
try
{
InitializeComponent();

myTable = new DataTable("NamesTable");
myTable.Columns.Add(new DataColumn("Name"));
DataColumn column = new DataColumn
("id", typeof(System.Int32));
myTable.Columns.Add(column);
myTable.Columns.Add(new
DataColumn("calculatedField", typeof(bool)));
DataSet namesDataSet = new DataSet();
namesDataSet.Tables.Add(myTable);
myGrid.SetDataBinding(namesDataSet, "NamesTable");

AddTableStyle();
AddData();
}
catch (System.Exception exc)
{
Console.WriteLine(exc.ToString());
}
}


private void grid_Enter(object sender, EventArgs e)
{
myGrid.CurrentCell = new DataGridCell(2,2);
}

private void AddTableStyle()
{
// Map a new TableStyle to the DataTable. Then
// add DataGridColumnStyle objects to the collection
// of column styles with appropriate mappings.
DataGridTableStyle dgt = new DataGridTableStyle();
dgt.MappingName = "NamesTable";

DataGridTextBoxColumn dgtbc = new DataGridTextBoxColumn();
dgtbc.MappingName = "Name";
dgtbc.HeaderText= "Name";
dgt.GridColumnStyles.Add(dgtbc);

dgtbc = new DataGridTextBoxColumn();
dgtbc.MappingName = "id";
dgtbc.HeaderText= "id";
dgt.GridColumnStyles.Add(dgtbc);

DataGridBoolColumnInherit db = new DataGridBoolColumnInherit();
db.HeaderText= "小于1000就变蓝";
db.Width= 150;
db.MappingName = "calculatedField";
db.AllowNull = false; //去掉灰色的可选状态
dgt.GridColumnStyles.Add(db);

myGrid.TableStyles.Add(dgt);

// This expression instructs the grid to change
// the color of the inherited DataGridBoolColumn
// according to the value of the id field. If it's
// less than 1000, the row is blue. Otherwise,
// the color is yellow.
db.Expression = "id < 1000";
}

private void AddData()
{
// Add data with varying numbers for the id field.
// If the number is over 1000, the cell will paint
// yellow. Otherwise, it will be blue.
DataRow dRow = myTable.NewRow();

dRow["Name"] = "name 1 ";
dRow["id"] = 999;
myTable.Rows.Add(dRow);

dRow = myTable.NewRow();
dRow["Name"] = "name 2";
dRow["id"] = 2300;
myTable.Rows.Add(dRow);

dRow = myTable.NewRow();
dRow["Name"] = "name 3";
dRow["id"] = 120;
myTable.Rows.Add(dRow);

dRow = myTable.NewRow();
dRow["Name"] = "name 4";
dRow["id"] = 4023;
myTable.Rows.Add(dRow);

dRow = myTable.NewRow();
dRow["Name"] = "name 5";
dRow["id"] = 2345;
myTable.Rows.Add(dRow);

myTable.AcceptChanges();
}

private void InitializeComponent()
{
this.myGrid = new System.Windows.Forms.DataGrid();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.myGrid)).BeginInit();
this.SuspendLayout();
//
// myGrid
//
this.myGrid.DataMember = "";
this.myGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.myGrid.Location = new System.Drawing.Point(48, 56);
this.myGrid.Name = "myGrid";
this.myGrid.Size = new System.Drawing.Size(400, 304);
this.myGrid.TabIndex = 0;
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(120, 416);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "checkBox1";
this.checkBox1.ThreeState = true;
//
// button1
//
this.button1.Location = new System.Drawing.Point(312, 408);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// MyForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(492, 473);
this.Controls.Add(this.button1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.myGrid);
this.Name = "MyForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
((System.ComponentModel.ISupportInitialize)(this.myGrid)).EndInit();
this.ResumeLayout(false);

}
[STAThread]
public static void Main()
{
MyForm myGridForm = new MyForm();
myGridForm.ShowDialog();
}

private void button1_Click(object sender, System.EventArgs e)
{

}
}

public class DataGridBoolColumnInherit : DataGridBoolColumn
{
private SolidBrush trueBrush = Brushes.Blue as SolidBrush;
private SolidBrush falseBrush = Brushes.Yellow as SolidBrush;
private DataColumn expressionColumn = null;
private static int count = 0;

public Color FalseColor
{
get
{
return falseBrush.Color;
}
set
{
falseBrush = new SolidBrush(value);
Invalidate();
}
}
xiaomaolover 2005-12-16
  • 打赏
  • 举报
回复
第二个问题
容易。
你可以在改变第一个值的时候。改变相关的记录。然后再帮定Datagrid
xiaomaolover 2005-12-16
  • 打赏
  • 举报
回复
datagrid的itembound事件里做
cehckbox ck = (checkbox)e.findcontrol(checkbox的ID)
if (flag = 1)
ch.enable = false;
.....
光亮1916 2005-12-16
  • 打赏
  • 举报
回复
加一个checked 属性就表示选中了
<input type="checkbox" name="c" value="1" checked>

110,502

社区成员

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

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

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