【问题标题】:Adding Combox in C# DataGridView (Winforms)在 C# DataGridView (Winforms) 中添加组合框
【发布时间】:2015-04-14 06:54:56
【问题描述】:

我的表单中几乎没有 DataGrid,我的数据源是 CSV 文件,我已经在其中设置了要显示的列。简而言之,当我选择新建项目时,CSV 文件将从默认位置加载,并显示一个带有空行和列名的网格。 现在我要做的是为第 1 列添加一个下拉列表(组合框)控件,如果是空网格,则保持为空,如果是带有数据的 CSV 文件,则动态显示数据,但我不知道该怎么做它。我从未在网格中添加组合框。包括 MSDN 在内的所有示例都令人困惑。 我是新手,非常欢迎明确的答案。 这是代码,我如何加载 CSV 数据

string[] strColumns = null;
string[] strData = null;

StreamReader sr = new StreamReader(strCSV);
DataTable dt = null;
int RowCount = 0;

while (!sr.EndOfStream)
{
    String strRow = sr.ReadLine().Trim();
    if (strRow.Length > 0)
    {
        strData = strRow.Split(delimter);

        if (RowCount == 0)
        {
            RowCount = 1;
            strColumns = strRow.Split(delimter);
            dt = new DataTable();

            foreach (string csvcolumn in strColumns)
            {
                DataColumn column = new DataColumn(csvcolumn.ToUpper(), typeof(string));
                column.DefaultValue = string.Empty;
                dt.Columns.Add(column);
            }
        }

        else
        {
            DataRow row = dt.NewRow();
            for (int i = 0; i < strColumns.Length; i++)
            {
                row[strColumns[i]] = strData[i] == null ? string.Empty : strData[i].ToString();
            }
            dt.Rows.Add(row);
        }
    }
}
sr.Close();
sr.Dispose();
return dt;

【问题讨论】:

  • 我不清楚你想要实现什么。您要添加DataGridViewComboBoxColumns 还是ComboBox(在DataGridView 之外)?
  • @stefankmitph 我想添加 DataGridViewComboBoxCell ,其中 ColumnIndex == 1

标签: c# winforms csv datagridview combobox


【解决方案1】:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.ColumnCount = 3;
        dataGridView1.Columns[0].Name = "Product ID";
        dataGridView1.Columns[1].Name = "Product Name";
        dataGridView1.Columns[2].Name = "Product Price";

        string[] row = new string[] { "1", "Product 1", "1000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "2", "Product 2", "2000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "3", "Product 3", "3000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "4", "Product 4", "4000" };
        dataGridView1.Rows.Add(row);

        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        cmb.HeaderText = "Select Data";
        cmb.Name = "cmb";
        cmb.MaxDropDownItems = 4;
        cmb.Items.Add("True");
        cmb.Items.Add("False");
        dataGridView1.Columns.Add(cmb);

    }
}

【讨论】:

  • 我认为代码非常全面。但我注意到了!
  • 我已经定义了列,我已经从空白 CSV 文件加载了这些列,并且行中可能有数据(在旧项目的情况下),而在新项目的情况下可能没有。我需要的是,在那些已经定义的列的单元格中添加下拉列表。
【解决方案2】:
             DataGridViewComboBoxCell comboJob = new DataGridViewComboBoxCell();

            //these data will be displayed in comboBox:

            string[] data = JobIdList.ToArray();

            comboJob.Items.AddRange(data);

            this.dgInfo()[e.ColumnIndex, e.RowIndex] = comboJob;

            bValidating = true;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 2013-08-10
    • 2014-09-11
    相关资源
    最近更新 更多