C#WINFORM datagridview 空白列数据绑定,数据行空白是怎么回事

C#实现WinForm DataGridView控件支持叠加数据绑定
我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支持,而不是实现,是因为他既可以是实现了IListSource的类型,也可以是实现了IList的类型,例如:List类型,DataTable类型等,这里就不一一列举了,今天我主要实现的功能如标题所描述的:实现WinForm DataGridView控件支持叠加数据绑定,或者说是附加数据功能,什么意思呢?说白了就是支持数据的多次绑定,标准的绑定方法只支持单一绑定,即每次绑定均会清除原来的数据,而叠加数据绑定则可实现每次绑定均以附加的形式(原数据保留)添加到DataGridView控件中,这样就实现了分页加载,但可完整显示已加载的所有数据,这种应用场景在C/S端很常见,B/S端上也有(例如QQ空间动态下面的加载更多按钮)
以下是实现附加数据两种方式:
第一种方式,采用反射获取属性值并循环添加数据行
private static void AppendDataToGrid(DataGridView grid, IList&object& source)
int rowCount = grid.Rows.C
List&DataGridViewRow& rows = new List&DataGridViewRow&();
Type t = source[0].GetType();
int rowIndex = grid.Rows.Add();
var girdCells = grid.Rows[rowIndex].C
//Common.ShowProcessing(&正在加载数据,请稍候...&, Common.MainForm, (o) =&
foreach (object item in source)
var row = new DataGridViewRow();
foreach (DataGridViewCell cell in girdCells)
var p = t.GetProperty(cell.OwningColumn.DataPropertyName);
object pValue = p.GetValue(item, null);
var newCell = (DataGridViewCell)cell.Clone();
newCell.Value = pV
row.Cells.Add(newCell);
rows.Add(row);
grid.Rows.RemoveAt(rowIndex);
grid.Rows.AddRange(rows.ToArray());
每二种方式,采用将数据源合并,然后重新绑定
protected void AppendDataToGrid&T,TResult&(DataGridView dataGridBase, IList&T& source,Func&T,TResult& orderBy) where T : class
//Stopwatch watch = new Stopwatch();
//watch.Start();
if (dataGridBase.Rows.Count & 0)
IEnumerable&T& bindsource =
Common.ShowProcessing(&正在加载数据,请稍候...&, Common.MainForm, (o) =&
var oldSource = (IList&T&)dataGridBase.DataS
bindsource = source.Concat(oldSource).OrderBy(orderBy).ToList();
dataGridBase.DataSource =
dataGridBase.DataSource =
//watch.Stop();
//MessageBox.Show(watch.ElapsedMilliseconds.ToString());
以上两种方法在代码量来看,第二种比较简单,第一种在执行效率上相对第二种方法要高,原因很简单,第一种每次处理的数据永远都是每页的数据,而第二种每次处理的数据是原有数据与现有数据的合集,随着数据量越多,加载也就越慢,大家也可以试一下,当然如果大家有其它更好的方法也可以分享一下。
为了体现面向对象以及可复用性,我将上述方法变为扩展方法,完整代码如下:
using System.Collections.G
using System.Windows.F
using System.D
namespace Zwj.Demo
public interface IAppendDataAble&out TControl& where TControl : Control
public class DataGridView2 : DataGridView, IAppendDataAble&DataGridView&
public static class AppendDataAbleControlExtension
public static void AppendData(this DataGridView grid, dynamic dataSource)
if (!(grid is IAppendDataAble&DataGridView&))
throw new Exception(&该DataGridView控件未实现IAppendDataAble&DataGridView&,无法使用该方法!&);
if (dataSource.GetType().IsValueType || dataSource == null)
grid.DataSource =
       Type interfaceType=dataSource.GetType().GetInterface(&System.Collections.IList&, true);
if (interfaceType!=null)
          List&object& list = new List&object&();
          list.AddRange(dataSource);
          AppendDataToGrid(grid, list);
else if (dataSource is DataTable)
AppendDataToGrid(grid, dataSource as DataTable);
/// &summary&
/// 附加数据到DataGridView(支持IList&T&类型的数据源)
/// &/summary&
/// &param name=&grid&&&/param&
/// &param name=&source&&&/param&
private static void AppendDataToGrid(DataGridView grid, IList&object& source)
int rowCount = grid.Rows.C
List&DataGridViewRow& rows = new List&DataGridViewRow&();
Type t = source[0].GetType();
int rowIndex = grid.Rows.Add();
var girdCells = grid.Rows[rowIndex].C
//Common.ShowProcessing(&正在加载数据,请稍候...&, Common.MainForm, (o) =&
foreach (object item in source)
var row = new DataGridViewRow();
foreach (DataGridViewCell cell in girdCells)
var p = t.GetProperty(cell.OwningColumn.DataPropertyName);
object pValue = p.GetValue(item, null);
var newCell = (DataGridViewCell)cell.Clone();
newCell.Value = pV
row.Cells.Add(newCell);
rows.Add(row);
grid.Rows.RemoveAt(rowIndex);
grid.Rows.AddRange(rows.ToArray());
/// &summary&
/// 附加数据到DataGridView(支持DataTable类型的数据源)
/// &/summary&
/// &param name=&grid&&&/param&
/// &param name=&table&&&/param&
private static void AppendDataToGrid(DataGridView grid, DataTable table)
int rowCount = grid.Rows.C
List&DataGridViewRow& rows = new List&DataGridViewRow&();
int rowIndex = grid.Rows.Add();
var girdCells = grid.Rows[rowIndex].C
//Common.ShowProcessing(&正在加载数据,请稍候...&, Common.MainForm, (o) =&
foreach (DataRow r in table.Rows)
var row = new DataGridViewRow();
foreach (DataGridViewCell cell in girdCells)
object pValue = r[cell.OwningColumn.DataPropertyName];
var newCell = (DataGridViewCell)cell.Clone();
newCell.Value = pV
row.Cells.Add(newCell);
rows.Add(row);
grid.Rows.RemoveAt(rowIndex);
grid.Rows.AddRange(rows.ToArray());
对代码稍微说明一下,为了避免扩展方法被滥用,即不需要附加数据的普通DataGridView造成影响,我定义了一个接口来规范它:IAppendDataAble&out TControl&,当然这个接口适用于所有控件,然后在扩展方法时AppendData加判断,如果实现了IAppendDataAble接口,则表明需要用到附加数据功能,就进行后面的处理,否则报错。我这里是基于DataGridView来扩展,大家也可以基于我定义的DataGridView2来扩展,这样更方便。另外,我上面实现了针对两种数据源类型进行了分别处理,以满足大多数的情况。
方法种注释掉的方法是我写的显示遮罩层的方法,如果大家需要,可以查看我的这篇博文:Winform应用程序实现通用遮罩层
&使用方法如下:
1.添加DataGridView控件,然后将DataGridView类型更改为DataGridView2类型,当然如果大家不需要进行扩展约束,那就无需更改DataGridView控件类型。
2.设置DataGridView列,将列的DataPropertyName设置为需要绑定的数据字段名称,这步很重要。
3.然后查询数据并调用扩展方法:
//dataGridView2Demo为DataGridView2类型
//dataSource为查询到的数据
dataGridView2Demo.AppendData(dataSource);
&为了提高扩展方法的执行效率,降低数据源类型判断及转换,我们也可以选择将扩展方法直接分为两个扩展方法,如下:
public static class ControlExtension
/// &summary&
/// 附加数据到DataGridView(支持IList&T&类型的数据源)
/// &/summary&
/// &typeparam name=&T&&&/typeparam&
/// &param name=&grid&&&/param&
/// &param name=&source&&&/param&
public static void AppendData&T&(this DataGridView grid, IList&T& source) where T : class
int rowCount = grid.Rows.C
List&DataGridViewRow& rows = new List&DataGridViewRow&();
Type t = typeof(T);
int rowIndex = grid.Rows.Add();
var girdCells = grid.Rows[rowIndex].C
Common.ShowProcessing(&正在加载数据,请稍候...&, Common.MainForm, (o) =&
foreach (object item in source)
var row = new DataGridViewRow();
foreach (DataGridViewCell cell in girdCells)
var p = t.GetProperty(cell.OwningColumn.DataPropertyName);
object pValue = p.GetValue(item, null);
var newCell = (DataGridViewCell)cell.Clone();
newCell.Value = pV
row.Cells.Add(newCell);
rows.Add(row);
grid.Rows.RemoveAt(rowIndex);
grid.Rows.AddRange(rows.ToArray());
/// &summary&
附加数据到DataGridView(支持DataTable类型的数据源)
/// &/summary&
/// &param name=&grid&&&/param&
/// &param name=&table&&&/param&
public static void AppendData(this DataGridView grid, DataTable table)
int rowCount = grid.Rows.C
List&DataGridViewRow& rows = new List&DataGridViewRow&();
int rowIndex = grid.Rows.Add();
var girdCells = grid.Rows[rowIndex].C
Common.ShowProcessing(&正在加载数据,请稍候...&, Common.MainForm, (o) =&
foreach (DataRow r in table.Rows)
var row = new DataGridViewRow();
foreach (DataGridViewCell cell in girdCells)
object pValue = r[cell.OwningColumn.DataPropertyName];
var newCell = (DataGridViewCell)cell.Clone();
newCell.Value = pV
row.Cells.Add(newCell);
rows.Add(row);
grid.Rows.RemoveAt(rowIndex);
grid.Rows.AddRange(rows.ToArray());
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'C#WINFORM DataGridView 数据绑定,数据行空白是怎么回事_百度知道
C#WINFORM DataGridView 数据绑定,数据行空白是怎么回事
提问者采纳
是直接指定的数据源,还是用for循环写入datagridview的先确定datatable里有没有数据,再看是如何绑定的
其他类似问题
为您推荐:
datagridview的相关知识
其他1条回答
列名和数据源的列名不一样
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C# Winform DataGridView 获取数据源帮顶表行状态的一点教训
如果类似于这样的绑定,
BindingSource bs = new BindingSource();& &
&&bs.DataSource =
& & &this.bindingNavigator1.BindingSource =
& & & this.dataGridView1.DataSource =
要获取 dt表的行状态。
千万记得在获取和调.dt.AcceptChanges();之前 &要&
dataGridView1.EndEdit();
& & & & & & bs.EndEdit();
否则会得到null的&
DataTable dtModify = dt.GetChanges(DataRowState.Modified);
& & & & & & DataTable dtDelete = dt.GetChanges(DataRowState.Deleted);
今天在这个地方浪费了很多时间,下次不要了。
> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!
Me.DataGridView1.CurrentCell.RowIndex
可以获取当前被选中的行号 可以定义一个静态全局的变量index 然后在SelectionChanged事件中为index=Me.DataGridView1.CurrentCell.RowIndex
这样就可以动态的在其他地方使用当前被选中的行号index了
me.DataGr ...
错误描述 对于dataGridView,设置数据源为一个List集合时,修改了List集合以后即使重新设置数据源界面也不会刷新. 注:如果先设置DataSource=可以重新加载,但是界面设计器里面设置的列名称,表头全部消失. 解决方法 设置数据源为DataTable 使用Rows.Add方法添加子项,需要重新加载数据时,先用Rows.Clear清 ...
这里介绍Oracle表空间状态,包括介绍查询Oracle表空间状态.更改Oracle表空间状态.修改数据文件的online/offline属性等方面. 本人很喜欢Oracle表空间,在工作中也很喜欢总结关于Oracle表空间状态的经验教训,下面就这个问题来详细说说吧. 1.查询Oracle表空间状态 select tablespace_name,status ...
WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件
.CS: 1 using S 2 using System.Collections.G 3 ponentM 4 using Sy ...
原文链接:http://blog.csdn.net/ibmfahsion/article/details/7891047 C# winform DataGridView 操作大全
http://blog.csdn.net/ibmfahsion 张迅雷总结: ---------------------------------------------- ...
声明:因为本文章是自己在开发过程中整理的资料,转载请注明出处
1.Winform 获取datagridview选中行的某列数据 for (int i = 0; i & dtGrdCurcy.SelectedRows.C i++)
string LSTR_Cunumber = dt ...
我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支持,而不是实现,是因为他既可以是实现了IListSource的类型,也可以是实现了IList的类型,例如:List类型,DataTable类型等,这里就不一一列 ...
C# DataGridView控件动态添加新行 DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行的两种方法: 方法一: int inde ...其他回答(4)
设置一下你的字段的FieldName吧。
收获园豆:10
额,搞定了,说来郁闷...不过确实很简单...
个人认为,数据源绑定正常、字段与单元格的绑定正常。
但是,绑定后的单元格数据却无法显示,经过调试发现空白的的单元格属性IsDataBound=false。
据MSDN说明,如果该列连接到数据源,则为 true;否则为 false。
进一步调试,AutoGenerateColumns = true时,DataGridView控件实例中,只有两个字段,但都有数据显示(关于数据定义的类,发文时没做说明,抱歉哦)....
经过比较发现,能正常显示数据的两列,所绑定的字段是&索引器&,如下所示:
public string ScanTypeName
if (ScanType == ProcessWatchCfg.ProcessScanType_Name)
return "进程名称";
else if (ScanType == ProcessWatchCfg.ProcessScanType_FullPath)
return "主模块全路径";
return "未知";
而未能正常显示数据的那些列,所绑定的字段是类属性,如下所示:
public string ProcessName
于是,做了调整,用于数据绑定的实体类,需要显示数据的字段,都增加了&索引器&....如下所示:
private string _ProcessN
/// &summary&
/// [IN]必填项。进程名称(不带.exe),系统用以向用户标识该进程的名称。
/// @注意:该进程名是不包括 .exe 扩展名或路径的进程友好名称,如 Outlook
/// Name of the process to be started.
/// &/summary&
public string ProcessName { get { return _ProcessN } set { _ProcessName = } }
然后,再次调试运行...OK!
运行结果如下:
问题还是出在DataPropertyName的设置上,呵呵~
收获园豆:30
园豆:28706
园豆:28706
楼主,请问索引器加在哪里?我是通过数据集绑定DataGridView控件,绑定之后字段就自动生成,没有自定义字段啊
/// &summary&
/// [IN]必填项。进程名称(不带.exe),系统用以向用户标识该进程的名称。
/// @注意:该进程名是不包括 .exe 扩展名或路径的进程友好名称,如 Outlook
/// Name of the process to be started.
/// &/summary&
public string ProcessName
{ get { return _ProcessN }
set { _ProcessName = }
/// &summary&
/// [IN]必填项。进程名称(不带.exe),系统用以向用户标识该进程的名称。
/// @注意:该进程名是不包括 .exe 扩展名或路径的进程友好名称,如 Outlook
/// Name of the process to be started.
/// &/summary&
public string ProcessName
{ get { return _ProcessN }
set { _ProcessName = }
/// &summary&
/// [IN]必填项。进程名称(不带.exe),系统用以向用户标识该进程的名称。
/// @注意:该进程名是不包括 .exe 扩展名或路径的进程友好名称,如 Outlook
/// Name of the process to be started.
/// &/summary&
public string ProcessName
{ get { return _ProcessN }
set { _ProcessName = }
&&&您需要以后才能回答,未注册用户请先。}

我要回帖

更多关于 datagridview绑定数据 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信