Tag: datagridviewcolumn

将DataGridViewComboBoxColumn.Valuemember设置为列表

我需要知道是否可以直接从字符串列表设置DataGridViewComboBoxColumn的ValueMember属性。 例如 List productNames = new List(); List t = new List(); foreach (var p in products) { var x = p.Product; itemListing = x; foreach (var pn in x) { productNames.Add(pn.name); } } …………. // set values to combobox column cells in datagridview GridSellProducts.Rows.Add(); DataGridViewComboBoxColumn cmbItems = (DataGridViewComboBoxColumn)GridSellProducts.Columns[“Item”]; cmbItems.DataSource = productNames; cmbItems.DisplayMember = cmbItems.ValueMember; […]

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。 干杯 亚伦

获取DataTable列DataType

DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn(gridColumn1, typeof(bool))); 我期待这一行的结果包含有关DataColumns Type(bool)的信息: ?dt.Columns[0].GetType()

c#如果行不为空,则更改颜色

我有一张桌子上有一些值。 如果行单元格“名称”不为空,则将背景颜色更改为紫色。 Name ID Customers Niky 1 yes // here change background to violet 2 no Donna 3 yes // here change background to violet Baka 4 no // here change background to violet 5 yes 6 no 我试过这段代码,但我不工作,不知道为什么: foreach (DataGridViewRow row1 in dataGridView1.Rows) { if (row1.Cells[0].Value != null) { row1.DefaultCellStyle.BackColor = Color.Violet; } }

在属性窗口中更改后,自定义属性将不会保存

我为DataGridView创建了一个自定义列,原因是我想在列中添加属性(类型)。 我右键单击DataGridView并选择“编辑列…”。 然后,当我选择我自定义列类型的列时,我可以编辑属性,但是如果我在编辑后单击“确定”,然后再次转到“编辑列…”,我分配给我的属性的值离开了。 这是我的代码: public class CustomColumn : DataGridViewColumn { [DisplayName(“Type”)] [Category(“Custom Property”)] public String type { get; set; } public CustomColumn() : base(new DataGridViewTextBoxCell()) { } } 和属性窗口的图像: 属性窗口的图像http://sofzh.miximages.com/c%23/Capture.png 有人可以告诉我我做错了什么,或者我需要添加什么,这样当我更改属性窗口中的值时,该值是否分配给属性?

如何按列名获取DataGridView的单元格值?

我有一个带有DataGridView的WinForms应用程序,DataSource是一个DataTable(从SQL Server填充),其xxx 。 以下代码引发了例外 ArgumentException未处理。 找不到名为xxx的列。 foreach (DataGridViewRow row in Rows) { if (object.Equals(row.Cells[“xxx”].Value, 123)) } 是否可以按列名获取单元格值?

在C#中合并RTL Datagridview列标题

我想合并3个Datagridview列标题(第3个,第4个和第5个) 列)和Datagridview的RightToleft属性已启用。 我是用户 这段代码: private void PromotionButton_Click(object sender, EventArgs e) { dataGridView1.ColumnHeadersHeight = dataGridView1.ColumnHeadersHeight * 2; dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.CellPainting += new DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting); dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint); dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll); dataGridView1.ColumnWidthChanged += new DataGridViewColumnEventHandler(dataGridView1_ColumnWidthChanged); } private void dataGridView1_Paint(object sender, PaintEventArgs e) { for (int j = 2; j -1) { Rectangle r2 = e.CellBounds; […]

如何在使用自定义DataSource时隐藏DataGridView的列?

我在c#中有一个小应用程序,它有一个使用以下内容填充的DataGridView: grid.DataSource = MyDatasource array; MyClass保存列的结构,它看起来像这样: class MyDatasource { private string column1; private string column2; public MyDatasource(string arg1, string arg2) { this.column1 = arg1; this.column2 = arg2; } public string column1 { get { return this.column1; } set { this.column1 = value; } } public string column2 { get { return this.column2; } set { […]

在列中的DataGridView中搜索值

我希望用户能够在DataGridView(dgv)的列中搜索数字。 dgv可以保存很多记录。 每条记录都有一个项目编号。 所以我希望用户能够在项目编号列中搜索项目编号。 我的列是:ProjectID(不可见); 图像(无headertext); 项目编号; 项目名; 公司; 联系。 这是我的代码: private void btnSearch_Click(object sender, EventArgs e) { string searchValue = textBox1.Text; int rowIndex = -1; dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect; try { foreach (DataGridViewRow row in dgvProjects.Rows) { if (row.Cells[row.Index].Value.ToString().Equals(searchValue)) { rowIndex = row.Index; dgvProjects.Rows[row.Index].Selected = true; break; } } } catch (Exception exc) { MessageBox.Show(exc.Message); […]