Tag: datagridview

如何使用SelectedRows从数据网格视图中获取选定的行数据?

我有一个表,我在数据网格视图控件中显示。 用户从控件中选择一行并按下按钮。 我需要从该行检索单元格并将它们存储为字符串。 究竟如何使用SelectedRow方法获取数据? 我已经在这个工作了好几个小时,我就在绳子的尽头。 这是我尝试过的一个例子: DataGridViewCellCollection selRowData = dataGridView1.SelectedRows[0].Cells; 如果我尝试访问selRowData [x],则返回值不包含我的数据。

通过在datagridviewcolum中指定DefaultCellStyle.Format值来显示百分比

With datagridview.Columns(“PricePerUnit”) .ValueType = Type.GetType(“System.Decimal”) .DefaultCellStyle.Format = “C” End With 数据表绑定到datagridview并在上面的代码中a如果我只是将值为5的行添加到“PricePerUnit”列,它将在datagridview列中显示为$ 5.00 同样地,我想要显示类似的内容如果我只是将值为5的行添加到“DiscountPercentage”列中 它应显示为5.00% 我需要一个字符串值来分配给DefaultCellStyle.Format来实现这一点。 如果我使用DefaultCellStyle.Format=”P”它会自动将其乘以100,因此对于输入5,它显示为500.00%而不是5.00% 有任何想法吗? 解决 dtb帮助我这样做(感谢他) number.ToString(“0.00\%”)获取十进制数和2个十进制整数

如何向datagridview添加新行?

我有DataGridView填充数据源(SQL)的数据。 现在我想添加一个新行,但我不能,因为新数据无法添加到有界DataGridView … 我试图: dataGridView1.Source = null; dataGridView1.Rows.Add(“1”); 但它清除了我之前在表格中的数据。 怎么做,添加新行而不删除以前的数据?

无法使用bindingsource刷新datagridview

目标: 单击“添加”或“删除”按钮后,应使用文档中的最新数据刷新datagridview。 问题: 通过删除或添加新数据进行更改后,无法刷新datagridview。 我正在使用与datagridview的数据源链接的绑定源。 我用不同的解决方案尝试了一切,并从不同的论坛阅读建议,但我仍然无法解决这个问题。 我也尝试使用这些语法“BindingSource.ResetBindings(false)”,“BindingSource.Refresh()”等但没有结果。 以下链接: 如何刷新绑定源 http://www.eggheadcafe.com/community/aspnet/2/10114324/datagridview-refresh-from-another-form.aspx http://blogs.msdn.com/b/dchandnani/archive/2005/03/15/396387.aspx http://bytes.com/topic/c-sharp/answers/812061-problem-refresh-datagridview bSrcStock.DataSource = myProductrepository.GetAllProductList(); dgridStock.DataSource = null; dgridStock.DataSource = bSrcStock; bSrcStock.ResetBindings(true); dgridStock.Columns[0].Width = 101; dgridStock.Columns[1].Width = 65; dgridStock.Columns[2].Width = 80; dgridStock.Columns[3].Width = 120; dgridStock.Columns[4].Width = 90;

如何使用C#中的文本框过滤datagridview?

我厌倦了使用文本框过滤数据网格视图,文本框包含在标签页中,但它不起作用,这里是代码: private void textBox1_TextChanged(object sender, EventArgs e) { try { ((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = “like ‘%” + textBox1.Text.Trim() + “%’ “; } catch (Exception) { } }

将值添加到特定的DataTable单元格

我想知道是否可以将值添加到特定的DataTable单元格? 假设我有一个现有的dataTable,并且我添加了一个新列,如何在不覆盖现有列的行的情况下添加到新列的行? 据我所知,没有一种方法可以添加到特定的细胞(除非我错了)。 dt.Rows.Add(a, b, c, d) 其中a,b,c和d是字符串值。 那么如果我只想添加到d列呢? 任何帮助,将不胜感激。

如何在datagridview中将第二列的两列数据相乘

我想将两列的数据相乘并在第三列中显示它。 例如: 1st Column: Quantity 2nd Column: Rate 3rd Column: Price 我希望乘以用户输入数量和Quantity=2的数据,如Quantity=2 , rate=50自动在价格列我希望100出现。 同时我想分割为用户输入数量和Price=100的数据,如Price=100 , rate=50自动在Quantity栏中我要2出现。 当用户输入数量和Price=100的数据,如Price=100 , Quantity=2自动在费率列我想要50出现。 这三个将在同一个datagridview中发生。 用户只能输入这三个中的任意两个字段,第三个字段将自动进入。 使用C#,VS2008,SQL 2008 private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e) { int quantity,rate; for (int i = 0; i < dataGridView2.Rows.Count; i++) { if(int.TryParse(dataGridView2.Rows[i].Cells[1].Value.ToString(), out quantity) && int.TryParse(dataGridView2.Rows[i].Cells[2].Value.ToString(), out rate)) { int price = quantity […]

DataGridView按钮单击返回错误的索引

简单的问题。 我有一个使用DataTable设置的dataGridView,然后在DataTable for buttons之后添加了2列。 当我点击一个索引为6的按钮时,它会给我索引1这是我的第一个按钮。 DataTable cartResults = cart.RunCartQuery(sb.ToString()); dgCart.DataSource = cartResults; // Add a button to the DataGridView at the specified position DataGridViewButtonColumn btnEdit = new DataGridViewButtonColumn(); btnEdit.Name = “btnEdit”; btnEdit.Text = “Edit”; btnEdit.HeaderText = “Edit Quantity”; btnEdit.UseColumnTextForButtonValue = true; dgCart.Columns.Insert((int)Buttons.Edit, btnEdit); DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn(); btnDelete.Name = “btnDelete”; btnDelete.Text = “Delete”; btnDelete.HeaderText […]

如何在DataGridView中查找具有列索引的列名?

我想在DataGridView找到列名。 我有列索引。 我怎么才能找到它。 dGVTransGrid.CurrentCell.ColumnIndex :我想要它的列名。 Plz的帮助。 谢谢。

如何在c#中将水平数据从数据库转换为垂直数据到datagridview

我在这个表格中有数据库中的学生标记表 ———————————- |RN| Student Name|Subjects|Marks| ———————————- |1 |Indra Poudel |DBMS |75 | |1 |Indra Poudel |SAD |75 | |1 |Indra Poudel |TOC |75 | |2 |Jon snow |DBMS |75 | |2 |Jon snow |SAD |75 | |2 |Jon snow |TOC |75 | ———————————- 现在我想以下面的forms在DataGridView中显示这些数据 ———————————- |RN| Student Name|DBMS|SAD |TOC| ———————————- |1 |Indra Poudel |75 |75 […]