繁体   English   中英

在C#DataGridView(Winforms)中添加Combox

[英]Adding Combox in C# DataGridView (Winforms)

我的表单中没有几个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;
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);

    }
}
             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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM