我正在設計一個winform,其中有一個帶有預定義列的datagridview,第一列是一個combobox,其余3列是textboxes。
我沒有將資料網格視圖系結到任何資料源,因為用戶將填寫最后一列 "數量",我想實作的是,當用戶點擊組合框時,它應該顯示資料庫表中的3列(專案代碼、專案名稱和uom),當用戶選擇任何特定的 "專案代碼 "時,相應的 "專案名稱 "和 "uom "應該顯示在資料網格視圖的第二列和第三列。同樣地,用戶應該能夠根據他的要求輸入任意多的行。資料輸入后,資料將被保存在一個名為 "請購單 "的表中。
到目前為止,我還沒有做任何編碼作業,只設計了表格。
uj5u.com熱心網友回復:
看看下面的內容是否能提供基礎代碼來滾動。有幾個類來模擬資料,還有一個擴展方法,應該移到自己的檔案中。
DataGridView沒有在設計器中配置,只是在代碼中配置。
using System;
using System.Collections.Generic;
namespace UnboundDataGridViewComboBox
{
public partial class Form1 : Form
{
private ComboBox _cbo;
private string _comboColumnName = "ItemCodeColumn"/span>;
private List<Item> _items => Mocked.Items。
public Form1()
{
InitializeComponent()。
Shown = OnShown。
}
private void OnShown(>objectsender, EventArgs e)。
{
var column1 = new DataGridViewComboBoxColumn
{
DataSource = _items.Select(x => x.ItemCode).ToArray()。
DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
Name = _comboColumnName,
HeaderText = "Item Code",
SortMode = DataGridViewColumnSortMode.NotSortable
};
var column2 = new DataGridViewTextBoxColumn
{
Name = "ItemNameColumn"。
HeaderText = "Item Name"var column3 = new DataGridViewTextBoxColumn
{
Name = "UomColumn"/span>,
HeaderText = "UOM".
};
var column4 = new DataGridViewTextBoxColumn
{
Name = "QuanityColumn"/span>,
HeaderText = "Quanity".
};
ItemsDataGridView.Columns.AddRange(column1, column2, column3, column4)。
ItemsDataGridView.EditingControlShowing = DataGridView1OnEditingControlShowing。
}
private void DataGridView1OnEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)。
{
if (! ItemsDataGridView.CurrentCell.IsComboBoxCell()) return;
if (ItemsDataGridView.Columns[ItemsDataGridView.CurrentCell.ColumnIndex].Name != _comboColumnName) return;
_cbo = e.Control as ComboBox;
_cbo.SelectedIndexChanged -= ItemCodeColumnComboSelectionChanged。
_cbo.SelectedIndexChanged = ItemCodeColumnComboSelectionChanged。
}
private void ItemCodeColumnComboSelectionChanged(object sender, EventArgs e)。
{
DataGridViewComboBoxEditingControl control = sender as DataGridViewComboBoxEditingControl;
Item item = _items.FirstOrDefault(x => x.ItemCode == control.EditingControlFormattedValue.ToString())。
if (item == null)
{
return;
}
ItemsDataGridView.CurrentRow.Cells[1].Value = item.Name。
ItemsDataGridView.CurrentRow.Cells[2].Value = item.UOM。
}
}
#region將類放入自己的檔案。
public static class Extensions
{
public static bool IsComboBoxCell(this DataGridViewCell sender)
=> sender.EditType != null &&
sender.EditType == typeof(DataGridViewComboBoxEditingControl)。
}
//代表資料庫中的一個表。
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public string ItemCode { get; set; }
public string UOM { get; set; }
public override string ToString() => 專案代碼。
}
class Mocked
{
//模擬來自資料庫的資料。
public static List< Item> Items => new List< Item>()
{
new Item() {Id = 1, Name = "P1"/span>, ItemCode = "A100"/span>, UOM = "Q1"/span>},
new Item() {Id = 2, Name = "P2"/span>, ItemCode = "A200"/span>, UOM = "W1"/span>},
new Item() {Id = 3, Name = "P3", ItemCode = "A300", UOM = "B1"},
new Item() {Id = 4, Name = "P4", ItemCode = "A400", UOM = "H1"}.
};
}
#endregion。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/309688.html
標籤:

