datagridview

DataGridView分页功能的实现

拟墨画扇 提交于 2020-02-25 19:20:21
1 // 1、定义几个所需的公有成员: 2 3 int pageSize = 0; //每页显示行数 4 int nMax = 0; //总记录数 5 int pageCount = 0; //页数=总记录数/每页显示行数 6 int pageCurrent = 0; //当前页号 7 int nCurrent = 0; //当前记录行 8 DataSet ds = new DataSet(); 9 DataTable dtInfo = new DataTable(); 10 11 //2、在窗体载入事件中,从数据源读取记录到DataTable中: 12 13 string strConn = "SERVER=127.0.0.1;DATABASE=NORTHWIND;UID=SA;PWD=ULTRATEL"; //数据库连接字符串 14 SqlConnection conn = new SqlConnection(strConn); 15 conn.Open(); 16 string strSql = "SELECT * FROM CUSTOMERS"; 17 SqlDataAdapter sda = new SqlDataAdapter(strSql,conn); 18 sda.Fill(ds,"ds"); 19 conn.Close(); 20 dtInfo = ds

Create chart from DataGridView

这一生的挚爱 提交于 2020-02-24 10:43:27
问题 I have a form with DataGridView (2 columns: PartnerName , Adult ) Please, I need to fill data from DataGridView to chart and print the chart 回答1: You can get idea from following code: private void DataGridBinding_Load(object sender, System.EventArgs e) { // Populate series data using random data double[] yValues = { 23.67, 75.45, 60.45, 34.54, 85.62, 32.43, 55.98, 67.23 }; for(int pointIndex = 0; pointIndex < yValues.Length; pointIndex++) { chart1.Series["Series1"].Points.AddXY(1990 +

设置datagridview中button按钮的背景颜色

无人久伴 提交于 2020-02-19 11:58:29
问题:DataGridViewButtonColumn()在datagridview中创建按钮列,如何设置按钮的背景颜色(不是单元格的背景颜色)。 回答:可以在dataGridView1_CellPainting事件里面处理。 private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.ColumnIndex == 0)//索引0 { e.Handled = true; using (SolidBrush brush = new SolidBrush(Color.Red)) { e.Graphics.FillRectangle(brush, e.CellBounds); } ControlPaint.DrawBorder(e.Graphics, e.CellBounds, Color.Yellow, ButtonBorderStyle.Outset); } if (e.ColumnIndex == 1)//索引1 { e.Handled = true; using (SolidBrush brush = new SolidBrush(Color.BlueViolet)) { e.Graphics.FillRectangle(brush, e

[Winform] DataGridView 中 DataGridViewComboBox 的可编辑

女生的网名这么多〃 提交于 2020-02-16 22:18:01
在 DataGridView 中设置的 DataGridViewComboBox,默认是不可编辑的,即使将其列属性 DisplayStyle 设置成 ComboBox 或其他,也无法编辑; 故作如下处理: 一 DataGridViewComboBoxCell 重写 DataGridViewComboBox 的单元控件 DataGridViewComboBoxCell 1 /// <summary> 2 /// 自定义可编辑下拉框单元 3 /// </summary> 4 public class DataGridViewComboEditBoxCell : DataGridViewComboBoxCell 5 { 6 public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, 7 DataGridViewCellStyle dataGridViewCellStyle) 8 { 9 base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); 10 11 ComboBox comboBox = (ComboBox)base.DataGridView

Set Format of auto-generated columns in DataGridView having object DataSource

对着背影说爱祢 提交于 2020-02-15 10:29:29
问题 I would like to auto create all the columns for my DataGridView based on my custom class. Every thing works like it should, but what I need is to format and align the cell values. So is there an attribute that I can add to my field (HeightMeter) so that it can align and format as required. To do this in a manual column create code, You will use the following: DataGridViewColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; DataGridViewColumn.DefaultCellStyle.Format =

Set Format of auto-generated columns in DataGridView having object DataSource

情到浓时终转凉″ 提交于 2020-02-15 10:28:50
问题 I would like to auto create all the columns for my DataGridView based on my custom class. Every thing works like it should, but what I need is to format and align the cell values. So is there an attribute that I can add to my field (HeightMeter) so that it can align and format as required. To do this in a manual column create code, You will use the following: DataGridViewColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; DataGridViewColumn.DefaultCellStyle.Format =

DataGridview清空数据

随声附和 提交于 2020-02-11 06:46:45
最近做的winform程序使用了DataGridView,在清除DataGridview的数据时遇到个问题。 我想要清空DataGridview的数据,用了DataGridview.Rows.Clear(),这时就出错了,提示“不能清除此列表”。 以前也遇到过,那时的解决办法就是重新绑定数据,也没仔细想为什么不能Rows.Clear()。于是搜索了下,用数据源绑定的DataGridView不能用Rows.Clear()清除,手动添加的是能够用clear()的。所以将datasource设置为null就可以清空数据,但是这不是我要的效果,这样会将DataGridView的列也删掉。想要保持原有的列用如下代码就可以了,就是重新绑定一个没有数据的datatable。 DataTable dt = (DataTable)dgvData.DataSource; dt.Rows.Clear(); dgvData.DataSource = dt; 如果用DataGridview.Rows.Clear()也能清空数据,和设置datasource为NULL(this.datagridview.datasoucre="null";)的效果是一样的。 来源: https://www.cnblogs.com/hongfei/archive/2012/12/24/2830812.html

C# DataGridView添加右键菜单等技巧(转载)

落花浮王杯 提交于 2020-02-10 20:07:39
1).右键点击行时选中行,并弹出操作菜单 1). 添加一个快捷菜单contextMenuStrip1; 2). 给dataGridView1的CellMouseDown事件添加处理程序: 程序代码 private void DataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right) { if (e.RowIndex >= 0) { //若行已是选中状态就不再进行设置 if (dataGridView1.Rows[e.RowIndex].Selected == false) { dataGridView1.ClearSelection(); dataGridView1.Rows[e.RowIndex].Selected = true; } //只选中一行时设置活动单元格 if (dataGridView1.SelectedRows.Count == 1) { dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; } //弹出操作菜单 contextMenuStrip1.Show(MousePosition.X,

Search a datagridview without doing an sql query

[亡魂溺海] 提交于 2020-02-08 05:16:45
问题 I have two DataGridview 's, dgvProducts and dgvCart . When I transfer a product to from dgvProducts to dgvCart , the specified quantity will deduct from the first datagridview. But the problem is my textbox search code, it is using a query so the datagridview quantity are reset everytime even when the transaction is not finish. This is the code for the search of textbox. private void textOrderSearch_TextChanged(object sender, EventArgs e) { if (textOrderSearch.Text != "") { crud.FillDataGrid(

WinForm\"DataGridView单元格提示和相关应用\" 之配餐系统的开发

£可爱£侵袭症+ 提交于 2020-02-08 00:32:31
在winform应用程序开发中,DataGridView是 用的较多的、也(几乎)是数据(列表)显示必用的控件,而此文就是针对DataGridView控件的一些使用做些讲解。 一. DataGridView单元格提示 这个熟悉的朋友会选择CellToolTipTextNeeded 事件实现,也确实,然而此事件有个硬性的条件(或者说 让人感觉很不舒服的限制),如图: 上图中 红色区域[具体见msdn:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.celltooltiptextneeded(VS.80).aspx]就是我所说的限制,这样当我们想实现用代码一行行add Row时,此事件就与我们想要的发生冲突(无法实现单元格提示) ——因为在 DataGridView绑定的数据源(设置了 DataSource )后 或 该控件的 VirtualMode 属性为 true (此时行数无法改变),不能再动态添加行,但我们想要的 单元格提示效果必须还得想办法实现(在最初遇到此问题时,感觉有点儿'山穷水尽疑无路'),而后静下心去想,终用DataGridView的CellMouseEnter事件解决,代码如下: 代码 canInfoDgv.CellMouseEnter += new