Tag: datagridview

当单元格离开时自动格式化datagridview的单元格

您好我想知道如何在每次进行一些更改时格式化2个小数位的单元格。 但我需要只格式化精确列,所以我认为这可能有效: private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e) { dataGridView1.Columns[2].DefaultCellStyle.Format = “n2”; } 但它没有格式化单元格。 为什么?

如何选择单元格的值?

我有一个DataGridView ,我想选择第一列的单元格。 Heres是我的datagridview.Click方法: private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { name = dataGridView1.CurrentRow.Cells[0].Value.ToString(); } 目前我的名字变量为null 。 我究竟做错了什么?

如何使用c#中的行索引和列索引从datagridview获取单元格值?

我有一个datagridview dgvList ..然后我想获取特定行和列的单元格值,而不使用selectedRows属性。 即: myvalue = dgvList[2nd row][1st column];

datagrid视图中的按键事件?

我需要编写一个简单的函数,当人输入框数时,然后按下按键事件number of boxes*someamount进入金额列。 我使用拖放控件添加了datagridview 我想根据我的研究,这里将编写代码 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { } 但我不知道如何把Keyup事件和访问列数字框numberofboxes and Amount 。 谢谢

如何在单元格上按住鼠标时在DataGridView中维护选定的行?

我试图在DataGridView中实现行移动。 我希望能够选择多行并单击任何选定行的单元格以开始拖动操作。 问题是当我将鼠标放在单元格上时,行将被取消选择。 我怎样才能防止这种情况发生?

如何将打印对话框添加到printpreviewdialog?

我的老板要我创建具有打印function的窗体,但他希望在预览后打印datagridview 。 所以现在我鼓励这个问题,当我点击printpreviewdialog上的打印按钮时,我无法打印多套纸张或选择打印机或进行任何更改。当我点击按钮时,它将直接打印纸张。 所以我希望加入printpreviewdialog和printdialog 。 为什么printpreviewdialog和printdialog只能用于不同的按钮? 当需要单击一个按钮进行预览并单击另一个按钮来打印多个设置并更改打印机时,缺乏可用性。 任何人都可以帮助我吗? PrintDialog类 DialogResult result = printDialog1.ShowDialog(); // If the result is OK then print the document. if (result == DialogResult.OK) { position = 0; pageno = 1; printDocument2.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20); printDocument2.OriginAtMargins = true; printPreviewDialog1.Document = printDocument2; printPreviewDialog1.ShowDialog(); } PrintPreviewDialog上 printDocument3.DefaultPageSettings.Margins = new Margins(20, 20, […]

editmode中的单元格不会在C#中触发OnKeyDown事件

我已经制作了自己的datagridview控件,其中包含ovveride OnKeyDown事件: public partial class PMGrid : DataGridView { protected override void OnKeyDown(KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { e.SuppressKeyPress = true; //suppress ENTER //SendKeys.Send(“{Tab}”); //Next column (or row) base.OnKeyDown(e); } else if (e.KeyCode == Keys.Tab) { base.OnKeyDown(e); this.BeginEdit(false); } else { base.OnKeyDown(e); } } } 当我点击datagridview并按Enter键时,它工作得很完美,因为行没有改变,而且KeyUp事件被触发。 但是当我按下Tab键时,会选择下一个单元格并将其更改为EditMode。 当我在这个单元格中按Enter键时,KeyUp事件不会被触发,KeyPress也会被触发。 我尝试这样做,用户可以从一个单元格移动到下一个单元格,然后用户可以在此单元格中写入内容,然后当用户按Enter键时,此值将保存到数据库中。 但是当单元格在EditMode中时,我无法检测到该用户按Enter键。 谢谢

Datagridview删除所有列

是否可以在单个匹配中从数据网格中删除所有列? 我知道我可以循环并逐个删除它们,或删除整个事物并创建它。 但是可以简单地清除列并将网格留在原位,以便用户可以重新开始。 即将其恢复到原始状态。 好的,这是我的代码 private void PopulateGrid() { UserVGrid.AutoGenerateColumns = false; UserVGrid.Columns.Clear(); List _values = populate.GetVaribles; foreach (var line in _values) { if (!line.Contains(“Startind”)) { string[] newline = line.Split(‘=’); DataGridViewColumn newColumn = new DataGridViewTextBoxColumn(); newColumn.Name = newline[0]; newColumn.HeaderText = newline[1]; newColumn.ToolTipText = newline[2]; UserVGrid.Columns.Add(newColumn); } } 所以我们假设_values有apple,pear作为值 运行此方法一次,我得到一个数据网格,其中包含两个名为apple和pear的列。 这次运行它第二次使用_values包含char,table。 我最终得到了4支苹果,梨,椅子和桌子。 我想要的是它第二次运行它清除网格然后应用新的colums。 干杯 亚伦

以编程方式更改DataGridView行上的ReadOnly模式

没有解释整个上下文,我的问题基本上是这样的: 我在Windows窗体上有一个datagridview,它绑定到Entity Framework DbSet: dbSet.Local.ToBindingList() 。 如果我将datagridview的ReadOnly属性设置为true(在设计视图中),然后在我的代码中包含此语句: myDataGridView.Rows[rowIndex].ReadOnly = false; 它直接通过而不改变价值! (不,我的数据源不是只读的。) 循环遍历行中的单元格并单独设置每个单元格的ReadOnly属性也不起作用: foreach (DataGridViewCell cell in myDataGridView.Rows[rowIndex].Cells) { cell.ReadOnly = false; } .ReadOnly – Gets or sets a value indicating whether the user can edit the cells of the DataGridView control. 但它表现得像某种原因无法设定 。 这有可能发生吗? 我在想,也许datagridview的ReadOnly属性会覆盖对特定行/单元格所做的任何更改(即整个表单必须是readonly或不是),但显然这适用于某人…… 还有其他方法可以实现我的最终目标,但我想知道为什么我不能改变这个属性,因为这对我的情况来说是最简单的解决方案。 编辑: 让我试着澄清一下我的问题: 同样,我知道还有其他方法可以实现我的最终目标,但是想要回答这个问题: 抱歉原始示例,但要轻松复制问题,创建一个WinForm应用程序,在其上抛出一个datagridview,并将此代码传递到您的项目中: public partial class Form1 […]

自定义DataGridView重复列

我通过从DataGridViewinheritance创建了一个自定义的Winforms控件。 自定义数据网格视图定义了它自己的列和映射。 但是,无论何时将其从工具箱拖到表单的设计图面上,表单都会为自定义控件中的每个列重新创建一个列控件(DataGridViewTextBoxColumn),这样我最终会得到我应该拥有的列数的两倍。 整个想法是控件已经“自我意识”,所以我不需要表单做任何事情。 很明显,我忽略了一些简单的事情,但我的网络比Windows更多,并没有立即突出的原因。 自定义数据网格: partial class StrategyGrid { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle13 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle14 = new System.Windows.Forms.DataGridViewCellStyle(); […]