datagridview

DataSet / BindingSource / DataGridView / BindingNavigator 的关系与绑定、更新顺序

限于喜欢 提交于 2020-01-09 21:55:03
1.数据源DataSet是保存数据,以及发布数据更新通知的核心。 2.BindingSource,是控件(DataGridView / BindingNavigator)与数据源交互的桥梁。BindingSource的DataSource指向数据源DataSet之后,数据源或控件的数据更改,则会被BindingSource传递到另一头。 3.DataGridView / BindingNavigator是数据控件,它们应该从BindingSource获取数据。当数据控件的数据,被用户更改后,则会通知BindingSource,接着BindingSource再通知数据源DataSet。 因此,这些组件绑定与更新的顺序为: 1.创建DataSet 2.把BindingSource的BindingSource绑定到DataSet。 3.把DataGridView的DataSource,以及BindingNavigator的BindingSource,都绑定到BindingSource。 关键:如果DataSet的数据发生变化,或者DataGridView / DataSource的数据发生变化,则双向的更改是自动进行与传递的。 但如果DataSet被替换,则需要把BindingSource的BindingSource重新绑定到DataSet。DataGridView 与

Merge RTL Datagridview columns header in C#

耗尽温柔 提交于 2020-01-09 11:56:02
问题 I want to merge 3 Datagridview columns headers (the 3rd, 4th, and the 5th columns) and the RightToleft property of the Datagridview is enabled. i user this code: private void PromotionButton_Click(object sender, EventArgs e) { dataGridView1.ColumnHeadersHeight = dataGridView1.ColumnHeadersHeight * 2; dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1

Add context menu in a datagrid view in a winform application

坚强是说给别人听的谎言 提交于 2020-01-09 11:10:22
问题 How to show a Context Menu when you right click a Menu Item in a DataGridView ? I would like to add delete in the menu so that the entire row can be deleted . Thanks in Advance 回答1: With reference to Miguel answer I think this will be easy to implement like this int currentRowIndex; private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) { currentRowIndex = e.RowIndex; } private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { dataGridView1.Rows

How could I Drag and Drop DataGridView Rows under each other?

北城余情 提交于 2020-01-09 06:15:05
问题 I've DataGridView that bound a List<myClass> and i sort it by " Priority " property in " myClass ". So I want to drag an " DataGridViewRow " to certain position to change it's " Priority " property. How could I "Drag & Drop" DataGridView Rows ?. And how to handle this ?. 回答1: I found this code sample on MSDN Note the following: 1). DataGridView property AllowDrop must be set to true (default is false). 2). The example below works out of the box when the DataGridView is NOT data-bound.

How could I Drag and Drop DataGridView Rows under each other?

做~自己de王妃 提交于 2020-01-09 06:14:27
问题 I've DataGridView that bound a List<myClass> and i sort it by " Priority " property in " myClass ". So I want to drag an " DataGridViewRow " to certain position to change it's " Priority " property. How could I "Drag & Drop" DataGridView Rows ?. And how to handle this ?. 回答1: I found this code sample on MSDN Note the following: 1). DataGridView property AllowDrop must be set to true (default is false). 2). The example below works out of the box when the DataGridView is NOT data-bound.

How could I Drag and Drop DataGridView Rows under each other?

南楼画角 提交于 2020-01-09 06:14:05
问题 I've DataGridView that bound a List<myClass> and i sort it by " Priority " property in " myClass ". So I want to drag an " DataGridViewRow " to certain position to change it's " Priority " property. How could I "Drag & Drop" DataGridView Rows ?. And how to handle this ?. 回答1: I found this code sample on MSDN Note the following: 1). DataGridView property AllowDrop must be set to true (default is false). 2). The example below works out of the box when the DataGridView is NOT data-bound.

DataGridView的添加、编辑、更新

最后都变了- 提交于 2020-01-09 05:27:56
今天说说DataGridView吧,在一个小项目中用到了这个。面对的问题是要在DataGridView中编辑、更新其中的值,然后怎么回写到数据库中?一开始的想法也是用循环读取Cell,然后自己手动回写到数据库中,但总觉得不方便,认为微软会把这些工作封装好的,就上网查了一下,发现确实有这个功能。先描述一下数据库中的结构 表1 表2 ID Name SexID ID Sex 1 A 1 1 男 2 B 2 2 女 3 C 2 一开始的绑定是很容易的,但绑定后更新不了数据库中的值,一更新就提示SexID的值是没设置数据源的,因为SexID是外键。还好在网上查到了一个折中的解决办法,是在建立一个DataTable自己做映射: DataTable dtRelation = new DataTable(); dtRelation.Columns.Add("Code",typeof(byte)); dtRelation.Columns.Add("DisplayString",typeof(string)); dtRelation.Rows.Add(new object[] { 0, "苹果" }); dtRelation.Rows.Add(new object[] { 1, "香蕉" }); dtRelation.Rows.Add(new object[] { 2, "西瓜" }); 2

DataGridView BindingSource BindNavigator

感情迁移 提交于 2020-01-09 05:27:44
代码 private void button1_Click(object sender, EventArgs e) { bindingNavigator1.BindingSource = bindingSource1; LoadData(); } private void LoadData() { // The xml to bind to. string xml = @"<US><states>" + @"<state><name>Washington</name><capital>Olympia</capital></state>" + @"<state><name>Oregon</name><capital>Salem</capital></state>" + @"<state><name>California</name><capital>Sacramento</capital></state>" + @"<state><name>Nevada</name><capital>Carson City</capital></state>" + @"</states></US>"; // Convert the xml string to bytes and load into a memory stream. byte[] xmlBytes = Encoding.UTF8

datagridview数据立即更新到数据库

天大地大妈咪最大 提交于 2020-01-09 02:03:59
一直想自己动手实现一个DATAGRIDVIEW实时更新数据到数据库,可是光想不做,都过去了很久。 正好趁今天下午有空,于是完成了一个,中间也碰到了不少问题。 言规正传,操作步骤如下: 1、新建一个表test_table,建立主键FID; create table TEST_TABLE ( FID INTEGER not null, FNAME NVARCHAR2(20), FLEVELID INTEGER ) alter table TEST_TABLE add constraint PRIMARY_KEY_TEST_TABLE primary key (FID) 2、由于是使用ORACLE,故添加了一个系列test_seq,以便添加数据时从这个系列取值; 3、使用dataadapter.update方法进行更新数据库,取数据的方法如下: public static DataInfo GetDataSet(string safeSql, string tblname, string primarykey) { DataSet ds = new DataSet(); DataTable dt = new DataTable(); OracleConnection connection = new OracleConnection(connectionString);

[C#] winform中的DataGridView的列宽设置(自动调整列宽)

纵饮孤独 提交于 2020-01-08 23:33:42
找了很多都说DataGridView有一个属性AutoSizeColumnMode,他有很多枚举值: 1、AllCells 调整列宽,以适合该列中的所有单元格的内容,包括标题单元格。 2、AllCellsExceptHeader 调整列宽,以适合该列中的所有单元格的内容,不包括标题单元格。 3、ColumnHeader 调整列宽,以适合列标题单元格的内容。 4、DisplayedCells 调整列宽,以适合当前屏幕上显示的行的列中的所有单元格的内容,包括标题单元格。 5、DisplayedCellsExceptHeader 调整列宽,以适合当前屏幕上显示的行的列中的所有单元格的内容,不包括标题单元格。 6、Fill 调整列宽,使所有列的宽度正好填充控件的显示区域,只需要水平滚动保证列宽在DataGridViewColumn.MinimumWidth 属性值以上。相对列宽由相对 DataGridViewColumn.FillWeight 属性值决定。 7、None 列宽不会自动调整。 8、NotSet 列的大小调整行为从 DataGridView.AutoSizeColumnsMode 属性继承。 使用方法举例: this . IssuesDataGridView . AutoSizeColumnsMode = System . Windows . Forms .