Datagrid绑定控件问题

dyss 2003-09-26 02:56:33
我看过"Henry手记— WinForm Datagrid结构剖析(三)"这片文章,里面只是介

绍如何绑定combobox(combobox的选项来自数据源).并没有谈到其他的控件,如

日期控件,自定义的COMBOBOX列,每行都显示COMBOBOX,不能指定只有那行才能

显示COMBOBOX,小弟需要的一些功能,并没有在那片文章中找到,想修改其中的

代码,无奈自己水平有限,而且任务很急,请求各位高手抽点时间帮帮小弟。
要求:
在SQL数据库里面有一TABLE,名为USERS。里面的字段有姓名(字符型)、国家(字

符型)、出生日期(日期型)、婚否(逻辑型)。
1.读取数据。从数据库取数据放在DATASET中的TUSER表中,经过处理TUSER表的行

列互换,行变成列,列变成行,添加到另外一个表TUSER1(TUSER1只是一个中间

表),再把TUSER1绑定到DATAGRID,DATAGRID显示为如下:
----------------------------
| 字段 | 值 |
----------------------------
| 姓名 | 小云 | 填写姓名时,不变
----------------------------
| 国家 | 中国 | 填写国家时,弹出COMBOBOX,
----------------------------
| 出生日期 | 2003-1-1 | 填写出生日期时,弹出日期控件,
----------------------------
| 婚否 | √ | 填写婚否时,弹出CHECKBOX控件,
----------------------------
2.上面的是要求,实现的方法大家想到的可能不同。其实,我要的是能够根据表

格里面的内容,弹出不同的控件给人选择,达到提高输入速度,和整体的一直。

不知道大家有没有用过“金碟“这套软件。里面的基本资料录入就是这样的。
...全文
62 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dyss 2003-09-29
  • 打赏
  • 举报
回复
如果我想一列都是checkbox ,但是其中有几行时textbox,那如何
比以上的要求应该容易的
91bct 2003-09-27
  • 打赏
  • 举报
回复
学习ing
up
charlse12 2003-09-27
  • 打赏
  • 举报
回复
太长了,不懂!!!
rock29 2003-09-26
  • 打赏
  • 举报
回复
Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Windows.Forms

Namespace DataGridTextBoxCombo
Public Class NoKeyUpCombo
Inherits ComboBox
Private WM_KEYUP As Integer = &H101


Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_KEYUP Then
'ignore keyup to avoid problem with tabbing & dropdownlist;
Return
End If
MyBase.WndProc(m)
End Sub 'WndProc
End Class 'NoKeyUpCombo
End Namespace
rock29 2003-09-26
  • 打赏
  • 举报
回复
Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data.SqlClient

Namespace DataGridTextBoxCombo
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
' use the derived nokeyup combo to avoid tabbing problem
Public WithEvents ColumnComboBox As NoKeyUpCombo
Private WithEvents _source As CurrencyManager
Private _rowNum As Integer
Private _isEditing As Boolean
Private components As System.ComponentModel.IContainer
'Fields
'Constructors
'Events
'Methods
Shared Sub New()
'Warning: Implementation not found
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Public Sub New() '初始化
MyBase.New()
_source = Nothing
_isEditing = True
ColumnComboBox = New NoKeyUpCombo()
AddHandler ColumnComboBox.Leave, New EventHandler(AddressOf LeaveComboBox)
AddHandler ColumnComboBox.SelectionChangeCommitted, New EventHandler(AddressOf ComboStartEditing)
End Sub

Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, readOnly1, instantText, cellIsVisible)
_rowNum = rowNum
_source = source
ColumnComboBox.Parent = Me.TextBox.Parent
ColumnComboBox.Location = Me.TextBox.Location
ColumnComboBox.Size = New Size(Me.TextBox.Size.Width, ColumnComboBox.Size.Height)
ColumnComboBox.Text = Me.TextBox.Text
Me.TextBox.Visible = False
ColumnComboBox.Visible = True
ColumnComboBox.BringToFront()
ColumnComboBox.Focus()
End Sub

Protected Overloads Overrides Function Commit(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) As Boolean
If _isEditing Then
_isEditing = False
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
End If
ColumnComboBox.Hide()
Return True
End Function

Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As EventArgs)
_isEditing = True
MyBase.ColumnStartedEditing(sender)
End Sub

Public Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)
Try
If _isEditing Then
SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text)
_isEditing = False
Invalidate()
End If
'ColumnComboBox.Hide()
Catch err As Exception
End Try
End Sub

End Class
End Namespace
rock29 2003-09-26
  • 打赏
  • 举报
回复
Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data.SqlClient

Namespace DataGridTextBoxCombo
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
' use the derived nokeyup combo to avoid tabbing problem
Public WithEvents ColumnComboBox As NoKeyUpCombo
Private WithEvents _source As CurrencyManager
Private _rowNum As Integer
Private _isEditing As Boolean
Private components As System.ComponentModel.IContainer
'Fields
'Constructors
'Events
'Methods
Shared Sub New()
'Warning: Implementation not found
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Public Sub New() '初始化
MyBase.New()
_source = Nothing
_isEditing = True
ColumnComboBox = New NoKeyUpCombo()
AddHandler ColumnComboBox.Leave, New EventHandler(AddressOf LeaveComboBox)
AddHandler ColumnComboBox.SelectionChangeCommitted, New EventHandler(AddressOf ComboStartEditing)
End Sub

Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, readOnly1, instantText, cellIsVisible)
_rowNum = rowNum
_source = source
ColumnComboBox.Parent = Me.TextBox.Parent
ColumnComboBox.Location = Me.TextBox.Location
ColumnComboBox.Size = New Size(Me.TextBox.Size.Width, ColumnComboBox.Size.Height)
ColumnComboBox.Text = Me.TextBox.Text
Me.TextBox.Visible = False
ColumnComboBox.Visible = True
ColumnComboBox.BringToFront()
ColumnComboBox.Focus()
End Sub

Protected Overloads Overrides Function Commit(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) As Boolean
If _isEditing Then
_isEditing = False
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
End If
ColumnComboBox.Hide()
Return True
End Function

Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As EventArgs)
_isEditing = True
MyBase.ColumnStartedEditing(sender)
End Sub

Public Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)
Try
If _isEditing Then
SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text)
_isEditing = False
Invalidate()
End If
'ColumnComboBox.Hide()
Catch err As Exception
End Try
End Sub

End Class
End Namespace
luoqing 2003-09-26
  • 打赏
  • 举报
回复
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(56, 24);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(320, 216);
this.dataGrid1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(456, 277);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

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

}

}

public delegate void ComboValueChanged( int changingRow, object newValue );


// Step 1. Derive a custom column style from DataGridTextBoxColumn
// a) add a ComboBox member
// b) track when the combobox has focus in Enter and Leave events
// c) override Edit to allow the ComboBox to replace the TextBox
// d) override Commit to save the changed data
public class DataGridComboBoxColumn : DataGridTextBoxColumn
{
public NoKeyUpCombo ColumnComboBox = null;
private System.Windows.Forms.CurrencyManager _source = null;
private int _rowNum;
private bool _isEditing = false;
ComboValueChanged _valueChanging;

public DataGridComboBoxColumn(ComboValueChanged valueChanging) : base()
{
_valueChanging = valueChanging;
ColumnComboBox = new NoKeyUpCombo();

ColumnComboBox.Leave += new EventHandler(LeaveComboBox);
ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged);
ColumnComboBox.SelectionChangeCommitted += new System.EventHandler(ComboStartEditing);

}

private void ComboStartEditing(object sender, EventArgs e)
{
_isEditing = true;
base.ColumnStartedEditing((Control) sender);
}

private void ComboIndexChanged(object sender, EventArgs e)
{
_valueChanging(_rowNum , ColumnComboBox.Text);
}

private void LeaveComboBox(object sender, EventArgs e)
{
if(_isEditing)
{
SetColumnValueAtRow(_source, _rowNum, ColumnComboBox.Text);
_isEditing = false;
Invalidate();
}
ColumnComboBox.Hide();
}

protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{


base.Edit(source,rowNum, bounds, readOnly, instantText , cellIsVisible);

_rowNum = rowNum;
_source = source;

ColumnComboBox.Parent = this.TextBox.Parent;
ColumnComboBox.Location = this.TextBox.Location;
ColumnComboBox.Size = new Size(this.TextBox.Size.Width, ColumnComboBox.Size.Height);
ColumnComboBox.SelectedIndexChanged -= new System.EventHandler(ComboIndexChanged);
ColumnComboBox.Text = this.TextBox.Text;
ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged);

this.TextBox.Visible = false;
ColumnComboBox.Visible = true;
ColumnComboBox.BringToFront();
ColumnComboBox.Focus();
}

protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
{
if(_isEditing)
{
_isEditing = false;
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text);
}
return true;
}


}

public class NoKeyUpCombo : ComboBox
{
const int WM_KEYUP = 0x101;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg == WM_KEYUP)
{
//ignore keyup to avoid problem with tabbing & dropdownlist;
return;
}
base.WndProc(ref m);
}
}
}


这些代码在codeproject中得到的
luoqing 2003-09-26
  • 打赏
  • 举报
回复
你可以看一下下面这段C#代码,你把COMBO换成日期控件看一下行不行


namespace DataGridTextBoxCombo
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataSet myDataSet;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

SetUp();
}

private void SetUp()
{
MakeDataSet();
dataGrid1.SetDataBinding(myDataSet, "Customers");
AddCustomDataTableStyle();
}

private void MakeDataSet()
{
// Create a DataSet.
myDataSet = new DataSet("myDataSet");

DataTable tCust = new DataTable("Customers");


// Create two columns, and add them to the first table.
DataColumn cCustID = new DataColumn("custID");
DataColumn cCustName = new DataColumn("custName");
DataColumn cCurrent = new DataColumn("custCity");
tCust.Columns.Add(cCustID);
tCust.Columns.Add(cCustName);
tCust.Columns.Add(cCurrent);

// Add the tables to the DataSet.
myDataSet.Tables.Add(tCust);


// Populates the table.
DataRow newRow1;

// Create three customers in the Customers Table.
for(int i = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = (100*i).ToString();
tCust.Rows.Add(newRow1);
}
// Give each customer a distinct name.
tCust.Rows[0]["custName"] = "John Summers";
tCust.Rows[1]["custName"] = "Phil Seagram";
tCust.Rows[2]["custName"] = "Sam Robinson";

// And address
tCust.Rows[0]["custCity"] = "Chicago";
tCust.Rows[1]["custCity"] = "Los Angeles";
tCust.Rows[2]["custCity"] = "Washington";
}

private void AddCustomDataTableStyle()
{
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "Customers";
// Set other properties.
ts1.AlternatingBackColor = Color.LightGray;
//
// Add 2 cols with textbox column style
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custID";
TextCol.HeaderText = "CustomerID";
TextCol.Width = 80;
ts1.GridColumnStyles.Add(TextCol);

TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custName";
TextCol.HeaderText = "Customer Name";
TextCol.Width = 100;
ts1.GridColumnStyles.Add(TextCol);

// Step 2 - Use the combo column style
// Add 1 col with combo style
DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn(new ComboValueChanged(MyComboValueChanged));
ComboTextCol.MappingName = "custCity";
ComboTextCol.HeaderText = "Customer Address";
ComboTextCol.Width = 100;
ts1.GridColumnStyles.Add(ComboTextCol);

// Step 3 - Additional setup for Combo style
// a) make the row height a little larger to handle minimum combo height
ts1.PreferredRowHeight = ComboTextCol.ColumnComboBox.Height + 3;
// b) Populate the combobox somehow. It is a normal combobox, so whatever...
ComboTextCol.ColumnComboBox.Items.Clear();
ComboTextCol.ColumnComboBox.Items.Add("Chicago");
ComboTextCol.ColumnComboBox.Items.Add("Corvallis");
ComboTextCol.ColumnComboBox.Items.Add("Denver");
ComboTextCol.ColumnComboBox.Items.Add("Great Falls");
ComboTextCol.ColumnComboBox.Items.Add("Kansas City");
ComboTextCol.ColumnComboBox.Items.Add("Los Angeles");
ComboTextCol.ColumnComboBox.Items.Add("Raleigh");
ComboTextCol.ColumnComboBox.Items.Add("Washington");


// c) set the dropdown style of the combo...
ComboTextCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

//add the custom table style to the datagrid table styles
dataGrid1.TableStyles.Add(ts1);


}

public void MyComboValueChanged(int rowChanging, object newValue)
{
Console.WriteLine("index changed {0} {1}", rowChanging, newValue);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

内容概要:本文介绍了软件定义汽车(SDV)的最佳实践案例,重点围绕基于Vector技术的电子电气(E/E)架构设计与实现。文档展示了高算力计算平台(HPC)、区域控制器(Zone ECU)和车载网络(如CAN、Ethernet)的系统架构布局,并结合AUTOSAR操作系统(Classic/Adaptive)、虚拟化(Hypervisor)和SOA服务设计,构建现代化车载系统。通过vCANdrive平台演示了从开发、测试(SIL/HIL)、到OTA升级的全流程,涵盖传感器、执行器、应用层软件及云端协同的集成方案。同时展示了硬件原型(如树莓派、Triboard)和MICROSAR系列工具链在实际项目中的应用。; 适合人群:从事汽车电子系统开发、车载软件架构设计以及智能网联汽车研发的工程师和技术管理人员,具备一定的嵌入式系统或AUTOSAR基础者更佳。; 使用场景及目标:①理解软件定义汽车的整体架构设计方法;②掌握基于Vector工具链的HPC与区域控制器集成方案;③实现OTA更新、SIL/HIL测试、ETH-CAN通信转换等关键技术验证;④支持智能驾驶(ADAS)与智能座舱(IVI)系统的快速原型开发。; 阅读建议:建议结合Vector相关工具(如PREEvision、CANoe4SW、MICROSAR)进行实践操作,重点关注系统分层设计、通信机制与软件更新流程,同时可参考文档中的硬件连接示意图与信号映射关系进行仿真与实车验证。

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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