文档视界 最新最全的文档下载
当前位置:文档视界 › dataGridView属性及方法

dataGridView属性及方法

dataGridView属性及方法
dataGridView属性及方法

Dat aGridView 类

请稍候

DataGridV iew成员

开启低带宽视图

全部折叠

此页面仅适用于

.NET Framework 3.0

同时提供下列产品的其他版本:

?Microsoft Visual Studio 2005/.NET Framework 2.0?Microsoft Visual Studio 2008/.NET Framework 3.5 DataGridView 成员

在可自定义的网格中显示数据。

下表列出了由DataGridView类型公开的成员。

公共构造函数

页首

公共属性

(请参见受保护的属性)

AccessibilityObject

AccessibleDefaultActionDescription

AccessibleDescription

AccessibleName

AccessibleRole

AdjustedTopLeftHeaderBorderStyle

AdvancedCellBorderStyle AdvancedColumnHeadersBorderStyle

AdvancedRowHeadersBorderStyle

AllowDrop

AllowUserToAddRows

AllowUserToDeleteRows

AllowUserToOrderColumns

AllowUserToResizeColumns

AllowUserToResizeRows

AlternatingRowsDefaultCellStyle

Anchor

AutoGenerateColumns AutoScrollOffset

AutoSize

AutoSizeColumnsMode AutoSizeRowsMode

BackColor

BackgroundColor BackgroundImage

BackgroundImageLayout

BindingContext

BorderStyle

Bottom

Bounds

CanFocus

CanSelect

Capture

CausesValidation

CellBorderStyle CheckForIllegalCrossThreadCalls

ClientRectangle

ClientSize ClipboardCopyMode ColumnCount

ColumnHeadersBorderStyle ColumnHeadersDefaultCellStyle ColumnHeadersHeight ColumnHeadersHeightSizeMode

ColumnHeadersVisible

Columns

CompanyName

Container

ContainsFocus

ContextMenu ContextMenuStrip

Controls

Created

Current

Current

CurrentRow

Cursor

DataBindings

DataMember

DataSource

DefaultBackColor

DefaultCellStyle

DefaultFont

DefaultForeColor

DisplayRectangle

Disposing

Dock

EditingControl

EditingPanel

EditMode

Enabled

EnableHeadersVisualStyles FirstDisplayedCell FirstDisplayedScrollingColumnHiddenWidth FirstDisplayedScrollingColumnIndex FirstDisplayedScrollingRowIndex

Focused

Font

ForeColor

GridColor

Handle

HasChildren

Height HorizontalScrollingOffset ImeMode

InvokeRequired

IsAccessible IsCurrentCellDirty IsCurrentCellInEditMode IsCurrentRowDirty IsDisposed IsHandleCreated IsMirrored

Item

LayoutEngine

Left

Location

Margin MaximumSize MinimumSize ModifierKeys MouseButtons MousePosition MultiSelect

Name NewRowIndex Padding Parent PreferredSize

ProductName ProductVersion

ReadOnly

RecreatingHandle

Region

Right

RightToLeft

RowCount RowHeadersBorderStyle RowHeadersDefaultCellStyle RowHeadersVisible

RowHeadersWidth

RowHeadersWidthSizeMode Rows RowsDefaultCellStyle RowTemplate

ScrollBars

SelectedCells SelectedColumns SelectedRows SelectionMode

ShowCellErrors

ShowCellToolTips ShowEditingIcon ShowRowErrors

Site

Size SortedColumn SortOrder StandardTab TabIndex TabStop

Tag

Text

Top TopLeftHeaderCell TopLevelControl

UserSetCursor

UseWaitCursor

VerticalScrollingOffset

VirtualMode

Visible

Width

WindowTarget

页首

受保护的属性

CanRaiseEvents

CreateParams

DefaultCursor

DefaultImeMode

DefaultMargin

DefaultMaximumSize

DefaultMinimumSize

DefaultPadding

DefaultSize

DesignMode

DoubleBuffered

Events

FontHeight

HorizontalScrollBar

RenderRightToLeft

ResizeRedraw

ScaleChildren

ShowFocusCues

ShowKeyboardCues

VerticalScrollBar

页首

公共方法

(请参见受保护的方法)

AdjustColumnHeaderBorderStyle

AreAllCellsSelected

AutoResizeColumn

AutoResizeColumnHeadersHeight

AutoResizeColumns

AutoResizeRow

AutoResizeRowHeadersWidth

AutoResizeRows

BeginEdit

BeginInvoke

BringToFront

C

ClearSelection

CommitEdit

Contains

CreateControl

CreateGraphics CreateObjRef

DisplayedColumnCount DisplayedRowCount Dispose

DoDragDrop DrawToBitmap

EndEdit

EndInvoke

Equals

FindForm

Focus

FromChildHandle FromHandle

GetCellCount GetCellDisplayRectangle GetChildAtPoint GetClipboardContent

GetColumnDisplayRectangle

GetContainerControl

GetHashCode GetLifetimeService

GetNextControl GetPreferredSize

GetRowDisplayRectangle GetType

Hide

HitTest InitializeLifetimeService Invalidate InvalidateCell InvalidateColumn InvalidateRow

Invoke

IsKeyLocked IsMnemonic

NotifyCurrentCellDirty PerformLayout

PointToClient

PointToScreen PreProcessControlMessage PreProcessMessage RectangleToClient RectangleToScreen ReferenceEquals

Refresh

RefreshEdit ResetBackColor ResetBindings

ResetCursor

ResetFont ResetForeColor

ResetImeMode ResetRightToLeft

ResetText ResumeLayout

Scale

Select

SelectAll

SelectNextControl

SendToBack

SetBounds

Show

Sort

SuspendLayout

ToString

Update

UpdateCellErrorText

UpdateCellValue

UpdateRowErrorText

UpdateRowHeightInfo

页首

受保护的方法

AccessibilityNotifyClients

AccessibilityNotifyCurrentCellChanged

AutoResizeColumn

AutoResizeColumnHeadersHeight

AutoResizeColumns

AutoResizeRow

AutoResizeRowHeadersWidth

AutoResizeRows ClearSelection CreateAccessibilityInstance

CreateColumnsInstance

CreateControlsInstance

CreateHandle CreateRowsInstance

DefWndProc

DestroyHandle

Dispose

Finalize

GetAccessibilityObjectById GetAutoSizeMode

GetScaledBounds GetService

GetStyle

GetTopLevel

InitLayout

InvokeGotFocus

InvokeLostFocus

InvokeOnClick

InvokePaint

InvokePaintBackground

IsInputChar

IsInputKey

MemberwiseClone

NotifyInvalidate OnAllowUserToAddRowsChanged OnAllowUserToDeleteRowsChanged OnAllowUserToOrderColumnsChanged OnAllowUserToResizeColumnsChanged OnAllowUserToResizeRowsChanged

OnAlternatingRowsDefaultCellStyleChange d

OnAutoGenerateColumnsChanged OnAutoSizeChanged

OnAutoSizeColumnModeChanged OnAutoSizeColumnsModeChanged

OnAutoSizeRowsModeChanged OnBackColorChanged

OnBackgroundColorChanged OnBackgroundImageChanged

OnBackgroundImageLayout

OnBindingContextChanged OnBorderStyleChanged OnCancelRowEdit OnCausesValidationChanged

OnCellBeginEdit OnCellBorderStyleChanged OnCellClick OnCellContentClick OnCellContentDoubleClick OnCellContextMenuStripChanged OnCellContextMenuStripNeeded OnCellDoubleClick OnCellEndEdit

OnCellEnter

OnCellErrorText OnCellErrorTextNeeded OnCellFormatting

OnCellLeave

OnC OnCellMouseDoubleClick OnCellMouseDown

OnCellMouseEnter

OnC

OnCellMouseMove

OnCellMouseUp

OnCellPainting

OnCellParsing OnCellStateChanged OnCellStyleChanged OnCellStyleContentChanged OnCellToolTipTextChanged OnCellToolTipTextNeeded OnCellValidated

OnCellValidating OnCellValueChanged OnCellValueNeeded OnCellValuePushed OnChangeUICues

OnClick

OnClientSizeChanged

OnColumnAdded OnColumnContextMenuStripChanged

OnColumnDataPropertyNameChanged

OnColumnDefault OnColumnDisplayIndexChanged OnColumnDividerDoubleClick OnColumnDividerWidthChanged

OnColumnHeaderCellChanged OnColumnHeaderMouseClick OnColumnHeaderMouseDoubleClick

OnColumnHeadersBorderStyleChanged

OnColumnHeadersDefaultCellStyleChange d

OnColumnHeadersHeightChanged

OnColumnHeadersHeightSizeModeChange d

OnColumnMinimumWidthChanged OnColumnNameChanged OnColumnRemoved OnColumnSortModeChanged OnColumnStateChanged OnColumnToolTipText OnColumnWidthChanged OnContextMenuChanged

OnContextMenuStripChanged

OnControlAdded

OnControlRemoved

OnCreateControl

OnCurrent

OnCurrent

OnCursorChanged

微软C#中DataGridView控件使用方法

DataGridView动态添加新行: DataGridView控件在实际应用中非常实用,特别需要表格显示数据时。可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行。假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行的两种方法: 方法一: int index=this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = "1"; this.dataGridView1.Rows[index].Cells[1].Value = "2"; this.dataGridView1.Rows[index].Cells[2].Value = "监听"; 利用dataGridView1.Rows.Add()事件为DataGridView控件增加新的行,该函数返回添加新行的索引号,即新行的行号,然后可以通过该索引号操作该行的各个单元格,如dataGridView1.Rows[index].Cells[0].Value = "1"。这是很常用也是很简单的方法。 方法二: DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell(); textboxcell.Value = "aaa"; row.Cells.Add(textboxcell); DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell(); row.Cells.Add(comboxcell); dataGridView1.Rows.Add(row);

DataGridView的用法

在C# WinForm下做过项目的朋友都知道,其中的DataGridView控件默认只支持DataGridViewButtonColumn、DataGridViewCheckBoxColumn、DataGridViewComboBoxColumn、DataGridViewImageColumn、DataGridViewLinkColumn和DataGridViewTextBoxColumn六种列类型,如果你想要在DataGridView的列中添加其它的子控件,则需要自己实现DataGridViewColumn和DataGridViewCell,这就意味着你需要从现有的列中继承并改写一些方法,如实现一个支持单选按钮的列,或支持三种选择状态的多选按钮的列。 上面两个截图分别为RadioButton列和支持三种状态的CheckBox列在DataGridView中的实现效果,我是在Windows 2003中实现的,因此显示的效果跟在XP和Vista下有些区别,Vista下CheckBox的第三种状态(不确定状态)显示出来的效果是一个实心的蓝色方块。 下面我看具体来看看如何实现这两种效果。 要实现自定义的DataGridView列,你需要继承并改写两个类,一个是基于DataGridViewColumn的,一个是基于DataGridViewCell的,因为

RadionButton和CheckBox的实现原理类似,因此我们可以将这两种列采用同一种方法实现。创建DataGridViewDisableCheckBoxCell和DataGridViewDisableCheckBoxColumn两个类,分别继承自DataGridViewCheckBoxCell和DataGridViewCheckBoxColumn。代码如下: public class DataGridViewDisableCheckBoxCell: DataGridViewCheckBoxCell { public bool Enabled { get; set; } // Override the Clone method so that the Enabled property is copied. public override object Clone() { DataGridViewDisableCheckBoxCell cell = (DataGridViewDisableCheckBoxCell)base.Clone(); cell.Enabled = this.Enabled; return cell; } // By default, enable the CheckBox cell. public DataGridViewDisableCheckBoxCell() { this.Enabled = true; } // Three state checkbox column cell protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { // The checkBox cell is disabled, so paint the border, background, and disabled checkBox for the cell. if (!this.Enabled) { // Draw the cell background, if specified. if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background) { SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor); graphics.FillRectangle(cellBackground,

DataGridView控件用法合集

DataGridView控件用法合集 目录 DataGridView控件用法合集(一) 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选中行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 DataGridView控件用法合集(二) 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 DataGridView控件用法合集(三) 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17.DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) DataGridView控件用法合集(四) 19. DataGridView中的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 DataGridView中输入错误数据的处理(五) 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 DataGridView控件用法合集(六) 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 DataGridView控件用法合集(七) 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定

vb6.0中DataGrid控件的使用

vb6.0中DataGrid控件的使用 https://www.docsj.com/doc/984704879.html,/ivu890103@126/blog/static/117734463201122782022384/ DataGrid 控件是一种类似于电子数据表的绑定控件,可以显示一系列行和列来表示 Recordset 对象的记录和字段。可以使用 DataGrid 来创建一个允许最终用户阅读和写入到绝大多数数据库的应用程序。DataGrid 控件可以在设计时快速进行配置,只需少量代码或无需代码。当在设计时设置了DataGrid 控件的 DataSource 属性后,就会用数据源的记录集来自动填充该控件,以及自动设置该控件的列标头。然后您就可以编辑该网格的列;删除、重新安排、添加列标头、或者调整任意一列的宽度。 在运行时,可以在程序中切换 DataSource 来察看不同的表,或者可以修改当前数据库的查询,以返回一个不同的记录集合。 注意 DataGrid 控件与 Visual Basic 5.0中的 DBGrid 是代码兼容的,除了一个例外:DataGrid 控件不支持 DBGrid 的“解除绑定模式”概念。DBGrid 控件包括在 Visual Basic 的 Tools 目录中。 可能的用法 查看和编辑在远程或本地数据库中的数据。 与另一个数据绑定的控件(诸如 DataList 控件)联合使用,使用 DataGrid控件来显示一个表的记录,这个表通过一个公共字段链接到由第二个数据绑定控件所显示的表。 使用 DataGrid 控件的设计时特性 可以不编写任何代码,只通过使用 DataGrid 控件的设计时特性来创建一个数据库应用程序。下面的说明概要地说明了在实现 DataGrid 控件的典型应用时的一般步骤。完整的循序渐进的指示,请参阅主题“DataGrid方案1: 使用 DataGrid 控件创建一个简单数据库应用程序”。 要在设计时实现一个 DataGrid 控件 1. 为要访问的数据库创建一个 Microsoft 数据链接 (.MDL) 文件。请参阅“创建 Northwind OLE DB 数据链接”主题,以获得一个示例。 2. 在窗体上放置一个 ADO Data 控件,并将其 ConnectionString 属性设置为在第 1 步中所创建的OLE DB 数据源。 3. 在这个 Ado Data 控件的 RecordSource 属性中输入一条将返回一个记 录集的 SQL 语句。例如,Select * From MyTableName Where CustID = 12 4. 在窗体上放置一个 DataGrid 控件,并将其 DataSource 属性设置为这个 ADO Data 控件。 5. 右键单击该 DataGrid 控件,然后单击“检索字段”。 6. 右键单击该 DataGrid 控件,然后单击“编辑”。 7. 重新设置该网格的大小、删除或添加网格的列。 8. 右键单击该 DataGrid 控件,然后单击“属性”。 9. 使用“属性页”对话框来设置该控件的适当的属性,将该网格配置为所需的外观和行为。 在运行时更改显示的数据

datagridview 数据处理方法 修改 删除 添加 下拉类表

Datagridview的三种处理数据方法 一、第一种方法 常规方法,在窗口界面上放入一个datagridview,在放各个textbox,然后通过选取对应的记录,修改textbox的值,所有的操作都在一个界面上进行,没什么多说的,大部分方法都这么做 二、弹出窗口方式 此方式,通过双击记录,或者是利用按钮操作,倾向于用按钮方式,一次修改或添加、删除一条记录。利用窗口传值方式,实现数据输入、输出,datagridview的显示跟新。 特点: 1。父子窗口之间的双向传值,很有参考意义 2.父子窗体监combox绑定数据表条件下,双向传值,很多资料接收的都不是很清晰, 主要是利用了combox.findstring()这个方法,传递回index,利用index得到value,好绕啊,废了很大劲。 3.datagridview修改、添加数据下,不用重新访问数据库,而是直接显示修改的结果, 这样感觉反应速度快,很有意义。 具体如下 修改界面

添加界面 主窗口代码 using System; using System.Collections.Generic; using https://www.docsj.com/doc/984704879.html,ponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using https://www.docsj.com/doc/984704879.html,monClass; namespace WDZ { public partial class frmMain2 : Form { public frmMain2() { InitializeComponent(); } private void frmMain2_Load(object sender, EventArgs e) { this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; //表格自适应宽度 //DataCon datacon = new DataCon(); //加载数据 DataOperate dataoperate = new DataOperate();

VB6.0中DataGrid的应用

使用DataGrid 控件 DataGrid 控件是一种类似于电子数据表的绑定控件,可以显示一系列行和列来表示Recordset 对象的记录和字段。可以使用DataGrid 来创建一个允许最终用户阅读和写入到绝大多数数据库的应用程序。DataGrid 控件可以在设计时快速进行配置,只需少量代码或无需代码。当在设计时设置了DataGrid 控件的DataSource 属性后,就会用数据源的记录集来自动填充该控件,以及自动设置该控件的列标头。然后您就可以编辑该网格的列;删除、重新安排、添加列标头、或者调整任意一列的宽度。 在运行时,可以在程序中切换DataSource 来察看不同的表,或者可以修改当前数据库的查询,以返回一个不同的记录集合。 注意DataGrid 控件与Visual Basic 5.0中的DBGrid 是代码兼容的,除了一个例外:DataGrid 控件不支持DBGrid 的“解除绑定模式”概念。DBGrid 控件包括在Visual Basic 的Tools 目录中。 可能的用法 查看和编辑在远程或本地数据库中的数据。 与另一个数据绑定的控件(诸如DataList 控件)联合使用,使用DataGrid控件来显示一个表的记录,这个表通过一个公共字段链接到由第二个数据绑定控件所显示的表。 使用DataGrid 控件的设计时特性 可以不编写任何代码,只通过使用DataGrid 控件的设计时特性来创建一个数据库应用程序。下面的说明概要地说明了在实现DataGrid 控件的典型应用时的一般步骤。完整的循序渐进的指示,请参阅主题“DataGrid 方案1: 使用DataGrid 控件创建一个简单数据库应用程序”。要在设计时实现一个DataGrid 控件 1. 为要访问的数据库创建一个Microsoft 数据链接(.MDL) 文件。请参阅“创建Northwind OLE DB 数据链接”主题,以获得一个示例。 2. 在窗体上放置一个ADO Data 控件,并将其ConnectionString 属性设置为在第1 步中所创建的OLE DB 数据源。 3. 在这个Ado Data 控件的RecordSource 属性中输入一条将返回一个记 录集的SQL 语句。例如,Select * From MyTableName Where CustID = 12 4. 在窗体上放置一个DataGrid 控件,并将其DataSource 属性设置为这个ADO Data 控件。 5. 右键单击该DataGrid 控件,然后单击“检索字段”。 6. 右键单击该DataGrid 控件,然后单击“编辑”。 7. 重新设置该网格的大小、删除或添加网格的列。 8. 右键单击该DataGrid 控件,然后单击“属性”。 9. 使用“属性页”对话框来设置该控件的适当的属性,将该网格配置为所需的外观和行为。在运行时更改显示的数据 在创建了一个使用设计时特性的网格后,也可以在运行时动态地更改该网格的数据源。下面介绍实现这一功能的通常方法。 更改DataSource 的RecordSource 更改所显示的数据的最通常方法是改变该DataSource 的查询。例如,如果DataGrid 控件使用一个ADO Data控件作为其DataSource,则重写RecordSource和刷新该ADO Data 控件都将改变所显示的数据。 ' ADO Data 控件连接的是Northwind 数据库的' Products 表。新查询查找所有 ' SupplierID = 12 的记录。

C#中DatagridView单元格动态绑定控件

C#中DatagridView单元格动态绑定控件 C#中DatagridView单元格动态绑定控件 我们在使用DatagridView的列样式的时候很方便,可以设置成comboboxcolumn,textboxcolumn等等样式,使用起来非常方便,但是,这样设置的列都采用同一种样式.对同一列采用多种样式的,就需要单独对单元格进行操作了. 具体方法如下: 1.实例化一个定义好的控件:如combobox 2.初始化combobox 控件3.获取private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.CurrentCell.ReadOnly == false && dataGridView1.CurrentCell.RowIndex == 2) // combobox显示条件 { comboBox1.Text = dataGridView1.CurrentCell.Value.ToString(); //对combobox 赋值R = dataGridView1.GetCellDisplayRectangle(dataGridView1.Curre ntCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false); //获取单元格位置

comboBox1.SetBounds(R.X + dataGridView1.Location.X, R.Y + dataGridView1.Location.Y, R.Width, R.Height); //重新定位combobox.中间有坐标位置的转换 comboBox1.Visible = true; } else comboBox1.Visible = false; } 4.将combobox的值写回到单元格 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { dataGridView1.CurrentCell.Value = comboBox1.Text; }

DataGridView实现数据的快速输入

C#利用DataGridView实现数据的快速输入 网络编程2008-03-11 16:04:03 阅读313 评论0 字号:大中小订阅 在做管理软件时,常常需要表格输入功能。表格输入极大地加快了数据输入,提高了工作效率,当然也提高了软件的竞争性。笔者最近用C#在做一套CRM时,成功地使用C# 2005里面的表格控件DataGridView 实现了表格输入功能,现在就把具体实现与各位分享: 1. 初始化工作 (1) 在Vs 2005 里面新建一个C# WinForm 应用程序:DataGridViewTest (2) 在窗体Form1上拖一个DataGridView控件:DataGridView1 (3) 在DataGridView1里添加两个列: Column1: 类型:DataGridViewComboBoxColumn HeaderText:时间 DataPropertyName:DutyTime Column2: 类型:DataGridViewTextBoxColumn HeaderText:时间 DataPropertyName:DutyTime (4)在Form1类中添加两个私有属性: private DataTable m_Table;//输入组合框控件的下拉数据 private DataTable m_DataTable;//与表格绑定的DataTable,即用户输入的最终数据 (5)在Form1类里面定义一个结构体 public struct MyRowData { public MyRowData(int no, string enDay, string cnDay) { No = no; EnDay = enDay; CnDay = cnDay; } public int No; public string EnDay; public string CnDay; } (6) 在Form1的load事件Form1_Load(object sender, EventArgs e) 加上以下初始化代码: this.dataGridView1.AllowUserToAddRows = true; this.dataGridView1.AllowUserToDeleteRows = true; this.dataGridView1.AutoGenerateColumns = false;

简单的DataGrid控件在WPF中绑定List集合数据

简单的DataGrid控件在中绑定List集合数据 1.在界面中添加DataGrid控件,用来显示系统的操作记录,界面和程序如下: 注释:AutoGenerateColumns这个属性为true时,控件的数据源list会按照自己的格式自动显示在控件上;如果这个属性为false,list的数据不会自动显示在datagrid上。 2.后台逻辑 List list = new List(); DateFilter filter = new DateFilter(Year,Month,Day); list = AllMananger.GetList(filter); //以上是我通过我的办法得到的list,要把此list绑定到DataGrid上。 this.operationGrid.ItemsSource = list; 现在把需要的list绑定到控件的ItemSource属性上。 3.Blinding 因为我的OperationRecord类中有三个属性,分别是OperationTime、OperationContent、OperationUser。 此时,把List list分别绑定到datagrid中的三个列中,按照我们对应的列名。例如:Binding="{Binding OperationTime }"

datagridview绑定数据源的几种常见方式

datagridview绑定数据源的几种常见方式datagridview绑定数据源的几种常见方式 //////////////开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定。 //////////////1)简单数据绑定 //////////////////using (SqlConnection conn = new SqlConnection(Config urationManager.ConnectionStrings["connStr"].ToString())) //////////////////{ ////////////////// SqlDataAdapter sda = new SqlDataAdapter("Select * Fr om T_Class Where F_Type='Product' order by F_RootID,F_Orders", conn); ////////////////// DataSet Ds = new DataSet(); ////////////////// sda.Fill(Ds, "T_Class"); ////////////////// //使用DataSet绑定时,必须同时指明DateMember ////////////////// //this.dataGridView1.DataSource = Ds; ////////////////// //this.dataGridView1.DataMember = "T_Class"; ////////////////// //也可以直接用DataTable来绑定 ////////////////// this.dataGridView1.DataSource = Ds.Tables["T_Class"]; //////////////////}

DATAGRID的用法

前几天打算尝试下DataGrid的用法,起初以为应该很简单,可后来被各种使用方法和功能实现所折磨。网络上的解决方法太多,但也太杂。没法子,我只好硬着头皮阅览各种文献资料,然后不断的去尝试,总算小有成果。因此,把我学到的和大家分享一下,相信这篇文章会让你再很短的时间内学会DataGrid的大部分主要功能,而且很多难点都可以在里面找到解决方案。 由于涉及的应用比较多,所以篇幅会很长。但可以确保各个版块相互独立,总共4个部分 1.数据绑定 2.DataGrid的增改删功能 3.DataGrid的分页实现 4.DataGrid的样式设计 先上一张截图,让你大概知道自己需要的功能是否在这张图里有所实现。 PS:使用技术:WPF+https://www.docsj.com/doc/984704879.html, Entity Framework

1.数据绑定(涉及DataGrid绑定和Combox绑定) 在DataGrid中同时包含“自动生成列”与“用户自定义列”由属性AutoGenerateColumns控制。 默认情况下,DataGrid将根据数据源自动生成列。下图列出了生成的列类型。 如果AutoGenerateColumns="True",我们只需要如下几行代码 后台dataGrid1.ItemsSource=infoList;//infoList为内容集合(这是我从数据库中获取的记录集合类型为List) PS:因为这里给dataGrid1绑定了数据源,所以下面绑定的字段都是infoList中的字段名称,同样也对应着我数据表中的字段名。里面包含FID,公司名称,职员姓名,性别,年龄,职务。解释下,怕大家无法理解Binding后面的值是如何来的了 显然这种数据绑定非常的容易,如果对表格要求不高,这中无疑是最简单方便的。

DataGridView中的数据在文本中显示

DataGridView中的数据在文本中显示 作者:逆命之心 实现汽车信息的查询和编辑,可根据输入的查询条件进行查询,也可以仅输入部分条件。单击“汽车基本信息”中的汽车信息,将在“汽车详细信息”中显示汽车的详细信息 1.在数据库中建一个表CaressInfo createtable CarsInfo ( CarId intprimarykeyidentity(1,1), Brand varchar(50)notnull,--品牌 Typevarchar(50)notnull,--型号 Dischaarge numeric(18,1)notnull,--排量 GearBox varchar(50)notnull check(GearBox='手动'or GearBox='自动'or GearBox='手自一体'),--变速箱 oilUse numeric(18,1)notnull,--理论耗油 Frice int notnull--报价 ) 2.创建窗体 变速箱里的内容为:不限,自动,手动,手自一体 3.代码

//DataSet实例化,声明SqlDataAdapter类型的dsa DataSet da = new DataSet(); SqlDataAdapter dsa; //声明Select方法用于查询 publicvoid Select() { this.da.Clear(); //品牌文本框(txtBrand),排量文本框(txtDischaarge),变速箱文本框(cmbGearBox) string str = this.txtBrand.Text.Trim(); string str1=this.txtDischaarge.Text.Trim(); string str2 = this.cmbGearBox.Text.Trim(); //判断各种查询条件 if (!str.Equals("") && !str1.Equals("") && !str2.Equals("")) { string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where Brand='" + str + "' and Dischaarge='" + str1 + "' and GearBox='" + str2 + "'"; dsa = new SqlDataAdapter(Sql, Dbhpler.con); dsa.Fill(da, "CarsInfo"); this.dgv.DataSource = da.Tables["CarsInfo"]; } if (!str.Equals("") && str1.Equals("") && str2.Equals("")) { string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where Brand='" + str + "' "; dsa = new SqlDataAdapter(Sql, Dbhpler.con); dsa.Fill(da, "CarsInfo"); this.dgv.DataSource = da.Tables["CarsInfo"]; } if (str.Equals("") && !str1.Equals("") && str2.Equals("")) { string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where Dischaarge='" + str1 + "'"; dsa = new SqlDataAdapter(Sql, Dbhpler.con); dsa.Fill(da, "CarsInfo"); this.dgv.DataSource = da.Tables["CarsInfo"]; } if (str.Equals("") && str1.Equals("") && !str2.Equals("")) { string Sql = "select Brand,Type,Dischaarge,GearBox,Frice from CarsInfo where GearBox='" + str2 + "'"; dsa = new SqlDataAdapter(Sql, Dbhpler.con); dsa.Fill(da, "CarsInfo");

DataGridView中数据存入数据库方法

DataGridView做了新的数据显示控件加入到了.Net 05中,其强大的编辑能力让其成为了数据显示中必不可少的控件。目前对于DataGridView中的更新讲的挺多的,但直接的插入数据好像讲的不是太多,下面就以我的例子说明一下。 1、首先新建一个项目。 2、建立一个数据库连接类LinkDataBase。因为数据库操作有很多都是重复性工作,所以我们写一个类来简化对数据库的操作。 using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Data.Sql; namespace Test ...{ class LinkDataBase ...{ //设置连接字符串 private string strSQL; //与数据库连接 private string connectionString = "Data Source=Localhost;Initial Catalog=Test;Integr ated Security=True"; private SqlConnection myConnection; private SqlCommandBuilder sqlCmdBld; private DataSet ds = new DataSet(); private SqlDataAdapter da; public LinkDataBase() ...{ } //根据输入的SQL语句检索数据库数据 public DataSet SelectDataBase(string tempStrSQL, string tempTableName) ...{ this.strSQL = tempStrSQL; this.myConnection = new SqlConnection(connectionString); this.da = new SqlDataAdapter(this.strSQL, this.myConnection); this.ds.Clear(); this.da.Fill(ds, tempStrSQL); //返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名 return ds; } //数据库数据更新(传DataSet和DataTable的对象) public DataSet UpdateDataBase(DataSet changedDataSet, string tableName) ...{ this.myConnection = new SqlConnection(connectionString);

NET新手指南:轻松自定义DataGridView控件

.NET新手指南:轻松自定义DataGridView控件 .NET DataGridView是一个便于使用的数据绑定控件。本文为.NET新手介绍了如何使用.NET配置向导VB Express自定义DataGridView控件。只需非常简单的修改以及一两行代码,便可以轻松实现交替颜色行,自定义排序功能以及显示编辑行。这样一个既可以浏览数据又可以编辑数据的窗体非常实用。 本文的目标读者是.NET新手。首先讲述如何创建一个新连接,然后讲述如何自定义结果控件,使用Visual Basic Express(VB Express)配置向导,本文将描述如何填充DataGridView控件,然后按照以下步骤进行提高: 1、行的显示颜色交替,构成一个绿色条效果; 2、禁用掉DataGridView内置的单列排序功能; 3、执行这个窗体时显示编辑行。 开始 VB Express提供了许多方法检索和操作外部数据,例如,只需要运行VB Express的配置向导就可以建立一个到MS Access 示例数据库Northwind.mdb中Customers的连接: 1、启动VB Express,然后在标准工具栏上点击新建项目按钮,在弹出的对话框中选择Windows Form Application; 2、在名称控件处输入一个有意义的名字,点击确定按钮; 3、点击解决方案资源管理器右下角的数据源标签,如果没有看到这个标签,从“数据”菜单中选择显示数据源即可; 4、点击新建数据源按钮,启动新建数据源配置向导; 5、点击下一步,数据库选项保持默认设置; 6、在下一个面板中点击新建连接; 7、在弹出的新建连接对话框中,点击修改,从弹出的修改数据源对话框中选择Access数据库文件,然后点击确定按钮; 8、在新建连接对话框中点击浏览,找到Northwind.mdb的位置(在Office目录的Samples文件夹下),然后点击确定按钮; 9、点击测试连接,然后点击确定按钮清除确认消息; 10、如果连接工作正常,点击确定返回向导窗口,然后点击下一步继续;

DataGridView同步更新到数据库

DataGridView同步更新到数据库 一。绑定数据SqlConnection con = null; SqlDataAdapter sa = null; SqlCommandBuilder sb = null; DataTable dt = null; BindingSource bs = null; 窗体时绑定 数据private void DataBingding_Load(object sender, EventArgs e) { con = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs;"); try { con.Open(); sa = new SqlDataAdapter("select * from jobs", con); sb = new SqlCommandBuilder(sa);//绑定SqlDataAdapter dt = new DataTable(); sa.Fill(dt); this.dataGridView1.DataSource = dt; bs = new BindingSource(); bs.DataSource = dt;//绑定BindingSource con.Close(); } catch (Exception ex) { con.Close(); throw ex; } } SqlConnection con = null; SqlDataAdapter sa = null; SqlCommandBuilder sb = null; DataTable dt = null; BindingSource bs = null; //加载窗体时绑定数据private void DataBingding_Load(object sender, EventArgs e) { con = new SqlConnection("server=.;uid=sa;pwd=sa;database=pubs;"); try { con.Open(); sa = new SqlDataAdapter("select * from jobs", con); sb = new SqlCommandBuilder(sa);//绑定

DataGridView自定义列

Winform下DataGridView控件自定义列System.Windows.Forms.DataGridView控件是net下,数据显示使用最多的控件之一,但是Datagridviewk控件列类型却仅仅只有6中 分别是button 、checkbox、combobox、image、link、textbox 等6种常见类型。这很难满足我们日常开发需要。如果需要复杂的应用,要么找第三方控件,要么只能自己开发。而功能强大的第三方控件往往是需要付费的。但我们开发需要的很可能只是简单的功能,如果为了某个简单功能而专门购买一个控件对于个人来说有些得不偿失。 那么我们只剩下自己开发一途。幸运的是DataGridView控件容许我们进行二次开发,可以自定义我们需要的控件列。下图就是自定义日期输入自定义列,通过下面的例子,你完全可以开发出自己需要的功能列。下面给出https://www.docsj.com/doc/984704879.html,和C#代码和原理 自定义列必须自己写三个类,这三个类必须继承系统标准的类或实现系统标准接口。这三个类实际上代表gridview控件中的列、列中的单元格、以及单元格中的具体控件 分别继承自系统 1、DataGridViewColumn 代表表格中的列 2、DataGridViewTextBoxCell 代表列中的单元格 3、IDataGridViewEditingControl 接口,单元格控件可以几本可以继承自任何标准控件或者自定义控件,但是必须实现IDataGridViewEditingControl 下面给出vb和C#的详细案例代码

一、C# 代码 using System; using System.Windows.Forms; public class CalendarColumn : DataGridViewColumn { public CalendarColumn() : base(new CalendarCell()) { } public override DataGridViewCell CellTemplate { get { return base.CellTemplate; } set { // Ensure that the cell used for the template is a CalendarCell. if (value != null && !value.GetType().IsAssignableFrom(typeof(CalendarCell ))) { throw new InvalidCastException("Must be a CalendarCell"); } base.CellTemplate = value; } } } public class CalendarCell : DataGridViewTextBoxCell { public CalendarCell() : base() { // Use the short date format. this.Style.Format = "d"; }

相关文档
相关文档 最新文档