如何在DataGridView中显示ComboBox?

发布于 2025-02-02 21:03:38 字数 729 浏览 4 评论 0原文

大家好,我有两个名为brand(id,name)product(id,brandid,name)的表格,我想在datagridview(id,名称,品牌)品牌列应为combobox,请参阅brandID in in 产品表和显示品牌名称来自brand表的记录的名称

如何做到

品牌表

“在此处输入图像说明”

产品表

sstatic.net/9jq6u.png“ rel =“ nofollow noreferrer”> < href =“ https://i.sstatic.net/jbinh.png” rel =“ nofollow noreferrer”>

Hello everyone I have two Tables named Brand(Id,Name) and Product(Id,BrandId,Name), I want showing Product list in DataGridView(Id,Name,Brand), Brand Column should be ComboBox that refer to BrandId in Product Table and shows Name Of Brand from records in Brand table

How can do it

Brand Table

enter image description here

Product Table

enter image description here

DataGridView

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蔚蓝源自深海 2025-02-09 21:03:38
// Create and populate DataTable

DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Brand", typeof(string));

// Populate it here

// With this once we set datasource of dgv, it will not generate columns automatically
dataGridView1.AutoGenerateColumns = false;

DataGridViewTextBoxColumn idCol = new DataGridViewTextBoxColumn()
{
    Name = "Id",
    HeaderText = "Id",
    DataPropertyName = "Id",
    Width = 50,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Id"
    }
};

DataGridViewTextBoxColumn nameCol = new DataGridViewTextBoxColumn()
{
    Name = "Name",
    HeaderText = "Name",
    DataPropertyName = "Name",
    Width = 200,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Name"
    }
};

DataGridViewComboBoxColumn brandCol = new DataGridViewComboBoxColumn()
{
    Name = "Brand",
    HeaderText = "Brand",
    DataPropertyName = "Brand",
    Width = 100,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Brand"
    },
    DataSource = new List<Tuple<int, string>>() {
        new Tuple<int, string>(1, "Brand 1"),
        new Tuple<int, string>(2, "Brand 2"),
        new Tuple<int, string>(3, "Brand 3")
    },
    DisplayMember = "Item2",
    ValueMember = "Item1"
};

// Now we add these columns to our dgv
dataGridView1.Columns.Add(idCol);
dataGridView1.Columns.Add(nameCol);
dataGridView1.Columns.Add(brandCol);

// and at last, we bind our data to it
dataGridView1.DataSource = dt;

您可以看到该列是我们的combobox使用与普通combobox相同的绑定系统。为此,我使用了该单元列出的列表来填充它,但是通常您会从数据库中加载数据。

可以说,您拥有这样的品牌类别:

public class Brand
{
    public int ID { get; set; } // important to be property, not variable
    public string Name { get; set; }
}

您从数据库加载数据并创建以下对象列表:

// I have done it manually
List<Brand> brandsList = new List<Brand>();
brandsList.Add(new Brand() { ID = 1, Name = "Brand 1 });
brandsList.Add(new Brand() { ID = 2, Name = "Brand 2 });
brandsList.Add(new Brand() { ID = 3, Name = "Brand 3 });

然后,您将将上层ComboBox列定义更改为:

DataGridViewComboBoxColumn brandCol = new DataGridViewComboBoxColumn()
{
    Name = "Brand",
    HeaderText = "Brand",
    DataPropertyName = "Brand",
    Width = 100,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Brand"
    },
    DataSource = brandsList,
    DisplayMember = "Name",
    ValueMember = "ID"
};
// Create and populate DataTable

DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Brand", typeof(string));

// Populate it here

// With this once we set datasource of dgv, it will not generate columns automatically
dataGridView1.AutoGenerateColumns = false;

DataGridViewTextBoxColumn idCol = new DataGridViewTextBoxColumn()
{
    Name = "Id",
    HeaderText = "Id",
    DataPropertyName = "Id",
    Width = 50,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Id"
    }
};

DataGridViewTextBoxColumn nameCol = new DataGridViewTextBoxColumn()
{
    Name = "Name",
    HeaderText = "Name",
    DataPropertyName = "Name",
    Width = 200,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Name"
    }
};

DataGridViewComboBoxColumn brandCol = new DataGridViewComboBoxColumn()
{
    Name = "Brand",
    HeaderText = "Brand",
    DataPropertyName = "Brand",
    Width = 100,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Brand"
    },
    DataSource = new List<Tuple<int, string>>() {
        new Tuple<int, string>(1, "Brand 1"),
        new Tuple<int, string>(2, "Brand 2"),
        new Tuple<int, string>(3, "Brand 3")
    },
    DisplayMember = "Item2",
    ValueMember = "Item1"
};

// Now we add these columns to our dgv
dataGridView1.Columns.Add(idCol);
dataGridView1.Columns.Add(nameCol);
dataGridView1.Columns.Add(brandCol);

// and at last, we bind our data to it
dataGridView1.DataSource = dt;

You can see that column which is our ComboBox use same binding system as normal combobox. For this purposle I used list of tuples to populate it, but you would normally load data from database for that part.

Lets say you have Brand class like this:

public class Brand
{
    public int ID { get; set; } // important to be property, not variable
    public string Name { get; set; }
}

You load data from database and create list of these objects:

// I have done it manually
List<Brand> brandsList = new List<Brand>();
brandsList.Add(new Brand() { ID = 1, Name = "Brand 1 });
brandsList.Add(new Brand() { ID = 2, Name = "Brand 2 });
brandsList.Add(new Brand() { ID = 3, Name = "Brand 3 });

Then you would change upper combobox column definition to:

DataGridViewComboBoxColumn brandCol = new DataGridViewComboBoxColumn()
{
    Name = "Brand",
    HeaderText = "Brand",
    DataPropertyName = "Brand",
    Width = 100,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Brand"
    },
    DataSource = brandsList,
    DisplayMember = "Name",
    ValueMember = "ID"
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文