Tag: datagridview

每按一次按钮添加行datagridview

我在usercontrol上有一个datagridview。 我创建了一个数据表,并将datagrid的源设置为此数据表。 我希望,在运行时,能够在每次按钮点击时添加gridview上我想要的行数。 我的代码: private DataTable CreateTable() { Datatable table=new Datatable(); table.Columns.Add(“Name”.ToString()); table.Columns.Add(“Size”.ToString()); DataRow dr = table.NewRow(); dr[“Name”] = “Mike”; DataRow dr2 = table.NewRow(); dr2[“Name”] = “Ryan; DataRow dr3 = table.NewRow(); dr3[“Name”] = “Taylor”; dr[“Size”] = ” one”; dr2[“Size”] = “two”; table.Rows.Add(dr); table.Rows.Add(dr2); table.Rows.Add(dr3); return table; //and on my constructor I set gridview.DataSource=Datatable; } //Code […]

C# – 用于在DataGridView.Rows上循环的Lambda语法

C#中用于循环DataGridView的每个DataGridViewRow的正确lambda语法是什么? 作为一个例子,假设函数根据Cells [0]中的某个值使行.Visible = false。

在单个datagridview中显示来自相关表的数据

我在db中有一些表: Items Manufacturers Categories Cities Regions ============== ================ ============ ======== ========== ItemId ManufacturerId CategoryId CityId RegionId ManufacturerId CityId NameCategory RegionId NameRegion CategoryId NameManufacturer NameCity NameItem Weight 我使用以下代码显示DataGridView中的项目列表: DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(“select * from Items”, connectionString); SqlCommandBuilder cmdBldr = new SqlCommandBuilder(da); da.Fill(ds, “Items”); dataGridView1.DataSource = ds.Tables[0]; 我还有一个按钮,使用以下代码保存DataGridView的更改: da.Update(ds, “Items”); 我想在datagridview中替换两列 […]

为什么我必须在DataGridViewRow上调用Cast 扩展方法?

这不起作用。 var result = from row in grid.Rows where (string) row.Cells[“COLUMN_1”].Value == “Test” select row.Cells[“COLUMN_2”].Value as int?; 但这样做。 var result = from row in grid.Rows.Cast() where (string) row.Cells[“COLUMN_1”].Value == “Test” select row.Cells[“COLUMN_2”].Value as int?; 据我所知,DataGridViewRowCollection是DataGridViewRows的集合。 那么为什么我必须先将每个成员转换为DataGridViewRow才能访问其属性? 它是否与DataGridViewRowCollection实现非通用IEnumerable但不是通用版本IEnumerable的事实有关? 或者它显然在内部使用ArrayList ? 它并没有给我带来任何实际问题,但我想更好地理解它。

使用C#将CSV文件导入List

我正在使用C#将CSV文件导入我的应用程序 目前我有一个1字段的CSV文件。 它工作得很好,但现在我想在同一个应用程序中添加一个3字段的CSV文件。 一旦数据存储到List中,我就将它绑定到我的DataGridView 这是我写的相关代码。 如果您发现任何不属于我的问题但可能有问题的问题,请随时大声说出来。 我总是希望学习和改进我的代码。 BindingList data = new BindingList(); private void importExcelFile() { TextFieldParser parser = new TextFieldParser(fileName); parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(“,”); while (!parser.EndOfData) { //Processing row string[] fields = parser.ReadFields(); foreach (string field in fields) { StringValue s = new StringValue(field); // Issue is here. It adds it to a single […]

更改DataGridViewComboBoxColumn的单元格的背景颜色

我使用DataGridViewComboBoxColumn类型的列创建了一个DataGridView对象,以允许用户从下拉列表中选择值。 如果用户选择“高”,我想为combobox的背面着色。 但是,它不会为combobox着色,只会为combobox值着色。 代码是: dgvOverallRisk.Rows[0].Cells[1].Style.ForeColor = Color.Aqua; dgvOverallRisk.Rows[0].Cells[1].Style.BackColor = Color.Red; 它看起来像这样:

WPF DataGrid。 更改单细胞背景

我有大约66列和4000行的数据表 每一行都有一些基于条件的着色 我是WPF的新手实际上我已经实现了一些基于条件的着色datagridview中的行但WPF作为组件DATA GRID 如何根据细胞价值实现基于细胞的着色? 在WPF中 早些时候我在Win表单中做了类似的事情 public DataGridView colorGridview(DataGridView G) { string[] cellsrequired = {“Colnames1”,””colname2}; foreach (DataGridViewRow item in G.Rows) { foreach (DataGridViewCell cell in item.Cells) { if (cellsrequired.Contains(cell.OwningColumn.HeaderText)) { string str = cell.FormattedValue.ToString().Trim(); // n / a if (str != “N/A”)// Or your condition { if (str == “SKIP”) { cell.Style.BackColor = Color.Orange; […]

如何在datagridviewcell上绘制自定义控件?

我想为datagridviewcell托管一个自定义控件。 我唯一的好参考是http://msdn.microsoft.com/en-us/library/7tas5c80.aspx 但是,我希望单元格显示我自己的用户控件 public class CustomCell : DataGridViewTextBoxCell { protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); } } 谁能指导我怎么做?

C#在datagridview行中着色相同的值

假设我有一个填充行的datagridview。 现在为了使某些数据更加清晰,我想为某些细胞的背景着色。 但是有一些警告,我想要着色的列数可能会有所不同。 为了使事情更清楚,我将草拟一个假的数据网格: Name Thing2 col1 col2 col3 tes test 1 1 2 t2t ers 3 3 3 der zoef 2 3 1 现在,col1-col3细胞需要着色,具体取决于它们的值。 第一列中的单元格将始终为绿色(按照惯例),偏离它的单元格将显示为红色。 因此,第一行将col1和col2颜色为绿色,col3为红色等。 有关如何最好地解决这个问题的任何想法?

如何优化DataGridView的性能

我的Windows应用程序表单上有一个DataGridView控件。 此DataGridView基于纯文本文件(由用户在运行时指定)填充。 因此,动态计算列数和行数。 现在每件事情都按预期工作正常唯一的问题是我的DataGridView需要花费大量时间来加载数据,有没有办法优化DataGridView的性能? 提示:通常,datagridview包含1024列和almos 100行。 以下是填充我的DataGridView的代码 dataGridView1.ColumnCount = nColumnCount; for (int i = 0; i < CurrPageLines.Length; i++) { string sCurrLinecontents = CurrPageLines[i]; int n = dataGridView1.Rows.Add(); for (int j = 0; j < /*nColumnCount*/sCurrLinecontents.Length; j++) { dataGridView1.Rows[n].Cells[j].Value = sCurrLinecontents[j]; } }