c# winform DataGridTextBoxColumn里加入CheckBox 问题???

accpyy 2006-03-08 06:35:06
GridColumnStylesCollection colStyle = datagrid名.TableStyles[0].GridColumnStyles;
//在第一列加入一个复选框
DataGridTextBoxColumn dgMaterial = (DataGridTextBoxColumn)colStyle[1];

ComboBox cb_Check;
cb_Check.Dock = DockStyle.Right;
cb_Check.BackColor = Control.DefaultBackColor;
cb_Check.Width = 25;
cb_Check.SelectedIndexChanged += new EventHandler(cb_Check_IndexChange);

dgMaterial.TextBox.Controls.Clear();
dgMaterial.TextBox.Controls.Add(cb_Check);

这样把控件加进去后,显示的不是控件而是true或false值,不知道大家遇到过同样的问题没有,请大家帮忙解决!
...全文
309 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
张赐 2006-03-13
  • 打赏
  • 举报
回复
1、可以在表中加一个bool类型的字段
2、可以增加一个表,只有一行一列,bool类型的,然后和你现在的表联合查询就可以,
select * form a,b...
cnjack 2006-03-13
  • 打赏
  • 举报
回复
學習...
marvelstack 2006-03-13
  • 打赏
  • 举报
回复
可以试一下这里的方法,
http://blog.csdn.net/zhzuo/archive/2006/02/27/611634.aspx
http://blog.csdn.net/zhzuo/archive/2004/05/31/22036.aspx
yf1025 2006-03-13
  • 打赏
  • 举报
回复
http://www.syncfusion.com/faq/winforms/Files/datagridcheckbox.zip
这是一个例子,可以下载看看
yf1025 2006-03-13
  • 打赏
  • 举报
回复
// code assumes you have a DataSet named myDataSet, a table named "EastCoastSales" and a DataGrid myDataGrid
//STEP 1: Create a DataTable style object and set properties if required.
DataGridTableStyle ts1 = new DataGridTableStyle();
//specify the table from dataset (required step)
ts1.MappingName = "EastCoastSales";
// Set other properties (optional step)
ts1.AlternatingBackColor = Color.LightBlue;
//STEP 2: Create a string column and add it to the tablestyle
DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custName"; //from dataset table
TextCol.HeaderText = "Customer Name";
TextCol.Width = 250;
ts1.GridColumnStyles.Add(TextCol);
//STEP 3: Create an int column style and add it to the tablestyle
//this requires setting the format for the column through its property descriptor
PropertyDescriptorCollection pdc = this.BindingContext
[myDataSet, "EastCoastSales"].GetItemProperties();
//now created a formated column using the pdc
DataGridDigitsTextBoxColumn csIDInt =
new DataGridDigitsTextBoxColumn(pdc["CustID"], "i", true);
csIDInt.MappingName = "CustID";
csIDInt.HeaderText = "CustID";
csIDInt.Width = 100;
ts1.GridColumnStyles.Add(csIDInt);
//STEP 4: Add the checkbox
DataGridColumnStyle boolCol = new DataGridBoolColumn();
boolCol.MappingName = "Current";
boolCol.HeaderText = "Info Current";
//uncomment this line to get a two-state checkbox
//((DataGridBoolColumn)boolCol).AllowNull = false;
boolCol.Width = 150;
ts1.GridColumnStyles.Add(boolCol);
//STEP 5: Add the tablestyle to your datagrid's tablestlye collection
myDataGrid.TableStyles.Add(ts1);
-渔民- 2006-03-13
  • 打赏
  • 举报
回复
mark
accpyy 2006-03-09
  • 打赏
  • 举报
回复
谢谢Samen168(卖女孩的小火柴),可是这也太复杂了,有没有其他的比较简单的解决办法呢?
怎么没有人帮助呢 晕啊
Samen168 2006-03-08
  • 打赏
  • 举报
回复
<STAThread()> _
Public Shared Sub Main()
Application.Run(New MyForm())
End Sub
End Class
End Namespace
[C#]
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

// This example shows how to create your own column style that
// hosts a control, in this case, a DateTimePicker.
public class DataGridTimePickerColumn : DataGridColumnStyle
{
private DateTimePicker myDateTimePicker = new DateTimePicker();
// The isEditing field tracks whether or not the user is
// editing data with the hosted control.
private bool isEditing;

public DataGridTimePickerColumn() : base()
{
myDateTimePicker.Visible = false;
}

protected override void Abort(int rowNum)
{
isEditing = false;
myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);
Invalidate();
}

protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
myDateTimePicker.Bounds = Rectangle.Empty;

myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);

if (!isEditing)
return true;

isEditing = false;

try
{
DateTime value = myDateTimePicker.Value;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception)
{
Abort(rowNum);
return false;
}

Invalidate();
return true;
}

protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
DateTime value = (DateTime)
GetColumnValueAtRow(source, rowNum);
if (cellIsVisible)
{
myDateTimePicker.Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
myDateTimePicker.Value = value;
myDateTimePicker.Visible = true;
myDateTimePicker.ValueChanged +=
new EventHandler(TimePickerValueChanged);
}
else
{
myDateTimePicker.Value = value;
myDateTimePicker.Visible = false;
}

if (myDateTimePicker.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}

protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, myDateTimePicker.PreferredHeight + 4);
}

protected override int GetMinimumHeight()
{
return myDateTimePicker.PreferredHeight + 4;
}

protected override int GetPreferredHeight(Graphics g,
object value)
{
return myDateTimePicker.PreferredHeight + 4;
}

protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
DateTime date = (DateTime)
GetColumnValueAtRow(source, rowNum);
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(date.ToString("d"),
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}

protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (myDateTimePicker.Parent != null)
{
myDateTimePicker.Parent.Controls.Remove
(myDateTimePicker);
}
if (value != null)
{
value.Controls.Add(myDateTimePicker);
}
}

private void TimePickerValueChanged(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(myDateTimePicker);
}
}
namespace DataGridColumnStyleExample
{
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;


public class MyForm : Form
{
private DataTable namesDataTable;
private DataGrid grid = new DataGrid();
public MyForm() : base()
{
InitForm();

namesDataTable = new DataTable("NamesTable");
namesDataTable.Columns.Add(new DataColumn("Name"));
DataColumn dateColumn = new DataColumn
("Date", typeof(DateTime));
namesDataTable.Columns.Add(dateColumn);
DataSet namesDataSet = new DataSet();
namesDataSet.Tables.Add(namesDataTable);
grid.DataSource = namesDataSet;
grid.DataMember = "NamesTable";
AddGridStyle();
AddData();
}

private void AddGridStyle()
{
DataGridTableStyle myGridStyle = new DataGridTableStyle();
myGridStyle.MappingName = "NamesTable";

DataGridTextBoxColumn nameColumnStyle =
new DataGridTextBoxColumn();
nameColumnStyle.MappingName = "Name";
nameColumnStyle.HeaderText= "Name";
myGridStyle.GridColumnStyles.Add(nameColumnStyle);

DataGridTimePickerColumn timePickerColumnStyle =
new DataGridTimePickerColumn();
timePickerColumnStyle.MappingName = "Date";
timePickerColumnStyle.HeaderText = "Date";
timePickerColumnStyle.Width = 100;
myGridStyle.GridColumnStyles.Add(timePickerColumnStyle);

grid.TableStyles.Add(myGridStyle);
}

private void AddData()
{

DataRow dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 1";
dRow["Date"] = new DateTime(2001, 12, 01);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 2";
dRow["Date"] = new DateTime(2001, 12, 04);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 3";
dRow["Date"] = new DateTime(2001, 12, 29);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 4";
dRow["Date"] = new DateTime(2001, 12, 13);
namesDataTable.Rows.Add(dRow);

dRow = namesDataTable.NewRow();
dRow["Name"] = "Name 5";
dRow["Date"] = new DateTime(2001, 12, 21);
namesDataTable.Rows.Add(dRow);

namesDataTable.AcceptChanges();
}

private void InitForm()
{
this.Size = new Size(500, 500);
grid.Size = new Size(350, 250);
grid.TabStop = true;
grid.TabIndex = 1;
this.StartPosition = FormStartPosition.CenterScreen;
this.Controls.Add(grid);
}
[STAThread]
public static void Main()
{
MyForm myForm1= new MyForm();
myForm1.ShowDialog();
}
}
}
Samen168 2006-03-08
  • 打赏
  • 举报
回复
下面的示例创建一个承载 DateTimePicker 控件的 DataGridColumnStyle。
[Visual Basic]
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel

' This example shows how to create your own column style that
' hosts a control, in this case, a DateTimePicker.
Public Class DataGridTimePickerColumn
Inherits DataGridColumnStyle
Private timePicker As New DateTimePicker()
' The isEditing field tracks whether or not the user is
' editing data with the hosted control.
Private isEditing As Boolean

Public Sub New()
timePicker.Visible = False
End Sub

Protected Overrides Sub Abort(ByVal rowNum As Integer)
isEditing = False
RemoveHandler timePicker.ValueChanged, _
AddressOf TimePickerValueChanged
Invalidate()
End Sub

Protected Overrides Function Commit _
(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) _
As Boolean
timePicker.Bounds = Rectangle.Empty

AddHandler timePicker.ValueChanged, _
AddressOf TimePickerValueChanged

If Not isEditing Then
Return True
End If
isEditing = False

Try
Dim value As DateTime = timePicker.Value
SetColumnValueAtRow(dataSource, rowNum, value)
Catch
End Try

Invalidate()
Return True
End Function

Protected Overloads Overrides Sub Edit( _
ByVal [source] As CurrencyManager, _
ByVal rowNum As Integer, _
ByVal bounds As Rectangle, _
ByVal [readOnly] As Boolean, _
ByVal instantText As String, _
ByVal cellIsVisible As Boolean)
Dim value As DateTime = _
CType(GetColumnValueAtRow([source], rowNum), DateTime)
If cellIsVisible Then
timePicker.Bounds = New Rectangle _
(bounds.X + 2, bounds.Y + 2, bounds.Width - 4, _
bounds.Height - 4)

timePicker.Value = value
timePicker.Visible = True
AddHandler timePicker.ValueChanged, _
AddressOf TimePickerValueChanged
Else
timePicker.Value = value
timePicker.Visible = False
End If

If timePicker.Visible Then
DataGridTableStyle.DataGrid.Invalidate(bounds)
End If
End Sub

Protected Overrides Function GetPreferredSize( _
ByVal g As Graphics, _
ByVal value As Object) As Size
Return New Size(100, timePicker.PreferredHeight + 4)
End Function

Protected Overrides Function GetMinimumHeight() As Integer
Return timePicker.PreferredHeight + 4
End Function


Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal value As Object) As Integer
Return timePicker.PreferredHeight + 4
End Function


Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer)
Paint(g, bounds, [source], rowNum, False)
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
Paint(g, bounds, [source], rowNum, Brushes.Red, Brushes.Blue, alignToRight)
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Dim [date] As DateTime = CType(GetColumnValueAtRow([source], rowNum), DateTime)
Dim rect As Rectangle = bounds
g.FillRectangle(backBrush, rect)
rect.Offset(0, 2)
rect.Height -= 2
g.DrawString([date].ToString("d"), Me.DataGridTableStyle.DataGrid.Font, foreBrush, RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom))
End Sub

Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
MyBase.SetDataGridInColumn(value)
If Not (timePicker.Parent Is Nothing) Then
timePicker.Parent.Controls.Remove(timePicker)
End If
If Not (value Is Nothing) Then
value.Controls.Add(timePicker)
End If
End Sub

Private Sub TimePickerValueChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.isEditing = True
MyBase.ColumnStartedEditing(timePicker)
End Sub
End Class

Namespace DataGridColumnStyleExample
Public Class MyForm
Inherits Form

Private namesDataTable As dataTable
Private myGrid As DataGrid = New DataGrid()
Public Sub New()

InitForm()

namesDataTable = New DataTable("NamesTable")
namesDataTable.Columns.Add(New DataColumn("Name"))
Dim dateColumn As DataColumn = _
New DataColumn("Date", GetType(DateTime))
namesDataTable.Columns.Add(dateColumn)
Dim namesDataSet As DataSet = New DataSet()
namesDataSet.Tables.Add(namesDataTable)
myGrid.DataSource = namesDataSet
myGrid.DataMember = "NamesTable"
AddGridStyle()
AddData()
End Sub

Private Sub AddGridStyle()
Dim myGridStyle As DataGridTableStyle = _
New DataGridTableStyle()
myGridStyle.MappingName = "NamesTable"

Dim nameColumnStyle As DataGridTextBoxColumn = _
New DataGridTextBoxColumn()
nameColumnStyle.MappingName = "Name"
nameColumnStyle.HeaderText = "Name"
myGridStyle.GridColumnStyles.Add(nameColumnStyle)

Dim timePickerColumnStyle As DataGridTimePickerColumn = _
New DataGridTimePickerColumn()
timePickerColumnStyle.MappingName = "Date"
timePickerColumnStyle.HeaderText = "Date"
timePickerColumnStyle.Width = 100
myGridStyle.GridColumnStyles.Add(timePickerColumnStyle)

myGrid.TableStyles.Add(myGridStyle)
End Sub

Private Sub AddData()
Dim dRow As DataRow = namesDataTable.NewRow()
dRow("Name") = "Name 1"
dRow("Date") = New DateTime(2001, 12, 1)
namesDataTable.Rows.Add(dRow)

dRow = namesDataTable.NewRow()
dRow("Name") = "Name 2"
dRow("Date") = New DateTime(2001, 12, 4)
namesDataTable.Rows.Add(dRow)

dRow = namesDataTable.NewRow()
dRow("Name") = "Name 3"
dRow("Date") = New DateTime(2001, 12, 29)
namesDataTable.Rows.Add(dRow)

dRow = namesDataTable.NewRow()
dRow("Name") = "Name 4"
dRow("Date") = New DateTime(2001, 12, 13)
namesDataTable.Rows.Add(dRow)

dRow = namesDataTable.NewRow()
dRow("Name") = "Name 5"
dRow("Date") = New DateTime(2001, 12, 21)
namesDataTable.Rows.Add(dRow)

namesDataTable.AcceptChanges()
End Sub

Private Sub InitForm()

Me.Size = New Size(500, 500)
myGrid.Size = New Size(350, 250)
myGrid.TabStop = True
myGrid.TabIndex = 1
Me.StartPosition = FormStartPosition.CenterScreen
Me.Controls.Add(myGrid)
End Sub

Samen168 2006-03-08
  • 打赏
  • 举报
回复
重写DataGridColumnStyle
内容概要:本文系统研究了基于交替方向乘子法(ADMM)的多主体综合能源系统分布式协同优化方法,旨在实现多个能源主体之间的高效、隐私保护的能量协同管理。通过建立各主体的局部优化模型,并引入一致性约束条件,利用ADMM算法将集中式优化问题分解为多个分布式子问题进行迭代求解,从而在保证各主体数据隐私的同时达成全局协同优化目标。研究涵盖了模型构建、算法实现、收敛性分析及数值仿真全过程,并提供了完整的Matlab代码实现,有效复现了相关学术成果,具有较强的可验证性与可拓展性。; 适合人群:具备一定数学建模能力与优化理论基础,熟悉Matlab编程,从事综合能源系统、分布式优化算法、电力系统调度等相关方向研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 掌握ADMM算法在多主体能源系统协同优化中的建模与实现方法;② 构建支持隐私保护的分布式能源调度系统;③ 利用开源代码开展算法改进、性能测试或应用于实际系统的二次开发与科研论文复现; 阅读建议:建议结合凸优化与分布式计算的基础知识,深入理解增广拉格朗日函数构造、对偶更新与收敛判据的设计原理,在代码调试过程中重点关注惩罚参数选取、变量分裂策略及通信机制的实现细节,以全面掌握算法的核心机制与工程应用要点。

111,129

社区成员

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

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

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