C# datagridview中的组合框

C# datagridview中的组合框,c#,winforms,visual-studio,datagridview,combobox,C#,Winforms,Visual Studio,Datagridview,Combobox,我想在datagridview中放置一个组合框。我尝试过不同的方法(创建一个var列表,创建一个arraylist,等等),但没有一种是有效的。问题是我的列已经存在,因为我的select查询在datagridview中显示了我的数据库。但该列是空的,因为在我的数据库中,该列用NULL值填充 我有两个选择:创建一个组合框并添加一个列,或者如果您知道如何做,将我的组合框链接到已经存在的列。显然,我需要帮助创建我的组合框 谢谢大家! 我的代码: public partial class Repair

我想在datagridview中放置一个组合框。我尝试过不同的方法(创建一个var列表,创建一个arraylist,等等),但没有一种是有效的。问题是我的列已经存在,因为我的select查询在datagridview中显示了我的数据库。但该列是空的,因为在我的数据库中,该列用NULL值填充

我有两个选择:创建一个组合框并添加一个列,或者如果您知道如何做,将我的组合框链接到已经存在的列。显然,我需要帮助创建我的组合框

谢谢大家!

我的代码:

public partial class Repair : Form
{


    public Repair()
    {
        Main pp = new Main();

        InitializeComponent();
        this.label4.Text = pp.label3.Text;

        SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
        maConnexion.Open();
        SqlCommand command = maConnexion.CreateCommand();
        SqlCommand command1 = maConnexion.CreateCommand();

        if (Program.UserType == "admin")
        {
            command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference,  RepairingTime FROM FailAndPass WHERE FComponent IS NOT NULL";
            command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE FComponent IS NOT NULL";

        }
        else
        {
            command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference,  RepairingTime FROM FailAndPass WHERE ReportingOperator IS NULL AND FComponent IS NOT NULL";
            command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE ReportingOperator IS NULL AND FComponent IS NOT NULL";

        }

        SqlDataAdapter sda = new SqlDataAdapter(command);
        SqlDataAdapter sda1 = new SqlDataAdapter(command1);
        DataTable dt = new DataTable();
        DataTable dt1 = new DataTable();
        sda.Fill(dt);
        sda1.Fill(dt1);

        DataColumn dcIsDirty = new DataColumn("IsDirty", typeof(bool));
        DataColumn dcIsDirty1 = new DataColumn("IsDirty", typeof(bool));
        dcIsDirty.DefaultValue = false;
        dcIsDirty1.DefaultValue = false;




        dataGridView1.DataSource = dt;
        dataGridView2.DataSource = dt1;


        DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
        combo.HeaderText = "FaultCodeByOp";
        combo.Name = "combo";
        ArrayList list = new ArrayList();
        list.Add("C-C");
        list.Add("C-O");
        combo.Items.AddRange(list.ToArray());
        dataGridView1.Columns.Add(combo);



        dt.Columns.Add(dcIsDirty);
        dt1.Columns.Add(dcIsDirty1);

        maConnexion.Close();

        dataGridView1.Columns[6].Visible = false;
        dataGridView2.Columns[3].Visible = false;

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            for(int i=0;i<4;i++)
            {
                dataGridView1.Columns[i].ReadOnly = true;

            }
        }
        foreach (DataGridViewRow row in dataGridView2.Rows)
        {
            for(int i=0;i<3;i++)
            {
                dataGridView2.Columns[i].ReadOnly = true;
            }
        }




    }




    private void button2_Click(object sender, EventArgs e)
    {


        this.Hide();
        Main ff = new Main();
        ff.Show();




    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {


        SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
        maConnexion.Open();
        string Var1 = textBox1.Text;
        SqlCommand command = maConnexion.CreateCommand();
        SqlCommand command1 = maConnexion.CreateCommand();


        if (Program.UserType == "admin")
        {
            if (textBox1.Text != String.Empty)
            {

                //command.Parameters.AddWithValue("@BoardName", Var1 + "%");
                //command.Parameters.AddWithValue("@Machine", Var1 + "%");
                command.Parameters.AddWithValue("@SerialNum", Var1 + "%");
                command1.Parameters.AddWithValue("@SerialNum", Var1 + "%");
                //command.Parameters.AddWithValue("@FComponent", Var1 + "%");
                //command.CommandText = "SELECT * FROM FailAndPass WHERE BoardName LIKE @BoardName OR Machine LIKE @Machine OR SerialNum LIKE @SerialNum OR FComponent LIKE @FComponent";

                command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, FaultCodeByOp, RepairingTime FROM FailAndPass WHERE SerialNum LIKE @SerialNum AND FComponent IS NOT NULL";
                command1.CommandText = "SELECT Machine, BoardName, BoardNumber FROM FailAndPass WHERE SerialNum LIKE @SerialNum And FComponent IS NOT NULL";
            }
        }
        else
        {
            if (textBox1.Text != String.Empty)
            {

                //command.Parameters.AddWithValue("@BoardName", Var1 + "%");
                //command.Parameters.AddWithValue("@Machine", Var1 + "%");
                command.Parameters.AddWithValue("@SerialNum", Var1 + "%");
                command1.Parameters.AddWithValue("@SerialNum", Var1 + "%");
                //command.Parameters.AddWithValue("@FComponent", Var1 + "%");
                //command.CommandText = "SELECT * FROM FailOnly WHERE (BoardName LIKE @BoardName OR Machine LIKE @Machine OR SerialNum LIKE @SerialNum OR FComponent LIKE @FComponent) AND ReportingOperator IS NULL  ";
                command.CommandText = "SELECT Message, FComponent, ReadValue, ValueReference, FaultCodeByOp, RepairingTime FROM FailAndPass WHERE  (SerialNum LIKE @SerialNum) AND ReportingOperator IS NULL AND FComponent IS NOT NULL  ";
                command1.CommandText = "SELECT DISTINCT Machine, BoardName, BoardNumber FROM FailAndPass WHERE (SerialNum LIKE @SerialNum) AND ReportingOperator IS NULL AND FComponent IS NOT NULL";
            }
        }

        if (!string.IsNullOrWhiteSpace(textBox1.Text))
        {

            SqlDataAdapter sda = new SqlDataAdapter(command);
            SqlDataAdapter sda1 = new SqlDataAdapter(command1);
            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            sda.Fill(dt);
            sda1.Fill(dt1);

            DataColumn dcIsDirty = new DataColumn("IsDirty", typeof(bool));
            DataColumn dcIsDirty1 = new DataColumn("IsDirty", typeof(bool));
            dcIsDirty.DefaultValue = false;
            dcIsDirty1.DefaultValue = false;
            dt.Columns.Add(dcIsDirty);
            dt1.Columns.Add(dcIsDirty1);

            dataGridView1.DataSource = dt;
            dataGridView2.DataSource = dt1;
            maConnexion.Close();

            dataGridView1.Columns[6].Visible = false;
            dataGridView2.Columns[3].Visible = false;

        }






    }

    private void button1_Click(object sender, EventArgs e)
    {

        SqlConnection maConnexion = new SqlConnection("Server= localhost; Database= Seica_Takaya;Integrated Security = SSPI; ");
        maConnexion.Open();

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {


            if ((row.Cells[6].Value != null) && (bool)row.Cells[6].Value)
            {

                SqlCommand command = maConnexion.CreateCommand();
                command = new SqlCommand("update FailAndPass set FaultCodeByOp=@Fault, RepairingDate=@RD, RepairingTime = @RT, ReportingOperator=@RO WHERE SerialNum=@Serial", maConnexion);
                command.Parameters.AddWithValue("@Fault", row.Cells[4].Value != null ? row.Cells[4].Value : DBNull.Value);
                command.Parameters.AddWithValue("@RD", DateTime.Today.ToString("d"));
                command.Parameters.AddWithValue("@RT", row.Cells[5].Value != null ? row.Cells[5].Value : DBNull.Value);
                command.Parameters.AddWithValue("@RO", this.label4.Text);
                command.Parameters.AddWithValue("@Serial", this.textBox1.Text);
                command.ExecuteNonQuery();


            }
        }

        maConnexion.Close();
        this.Hide();
        Repair rep = new Repair();
        rep.Show();

    }



    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {

        if (dataGridView1.IsCurrentRowDirty)
        {
            dataGridView1.Rows[e.RowIndex].Cells[6].Value = true;
        }

    }
}
公共部分类修复:表单
{
公共维修()
{
主pp=新的主();
初始化组件();
this.label4.Text=pp.label3.Text;
SqlConnection maConnexion=newsqlconnection(“Server=localhost;Database=Seica_Takaya;integratedsecurity=SSPI;”);
maConnexion.Open();
SqlCommand=maConnexion.CreateCommand();
SqlCommand command1=maConnexion.CreateCommand();
if(Program.UserType==“admin”)
{
command.CommandText=“选择消息、FComponent、ReadValue、ValueReference、Failure的修复时间和FComponent不为空的Pass”;
command1.CommandText=“在FComponent不为NULL的情况下,从FailAndPass中选择机器、BoardName、BoardNumber”;
}
其他的
{
command.CommandText=“选择消息、FComponent、ReadValue、ValueReference、Failure的修复时间和Pass,其中ReportingOperator为NULL且FComponent不为NULL”;
command1.CommandText=“在ReportingOperator为NULL且FComponent不为NULL的情况下,从Failand Pass中选择机器、BoardName、BoardNumber”;
}
SqlDataAdapter sda=新SqlDataAdapter(命令);
SqlDataAdapter sda1=新的SqlDataAdapter(command1);
DataTable dt=新的DataTable();
DataTable dt1=新DataTable();
sda.填充(dt);
sda1.填充(dt1);
DataColumn dcIsDirty=新的DataColumn(“IsDirty”,typeof(bool));
DataColumn dcIsDirty1=新的DataColumn(“IsDirty”,typeof(bool));
dcIsDirty.DefaultValue=false;
dcIsDirty1.DefaultValue=false;
dataGridView1.DataSource=dt;
dataGridView2.DataSource=dt1;
DataGridViewComboBoxColumn组合=新建DataGridViewComboxColumn();
combo.HeaderText=“FaultCodeByOp”;
combo.Name=“combo”;
ArrayList=新建ArrayList();
列表。添加(“C-C”);
列表。添加(“C-O”);
combo.Items.AddRange(list.ToArray());
dataGridView1.Columns.Add(组合);
dt.Columns.Add(dcIsDirty);
dt1.Columns.Add(dcIsDirty1);
maConnexion.Close();
dataGridView1.Columns[6]。Visible=false;
dataGridView2.Columns[3]。Visible=false;
foreach(dataGridView1.Rows中的DataGridViewRow行)
{

对于(int i=0;i有人有其他解决方案吗?我尝试了其他方法:

        DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();


        ArrayList list1 = new ArrayList(); //{ "C-C", "C-O", "Absence composant", "Mauvaise valeur", "Mauvais sens", "Mauvais composant" };
        list1.Add("C-C");
        list1.Add("C-O");


        combo.DataSource = list1;
        combo.HeaderText = "FaultCodeByOp";
        combo.DataPropertyName = "FaultCodeByOp";


        dataGridView1.Columns.AddRange(combo); 

没有更改任何内容。

我不知道我做了什么,但它不再是灰色的。现在它像其他列一样是白色的……但它不会下拉,也不会显示列表中的成员。

您好,很抱歉回复太晚。我在度假。我理解代码。它说“Var rowindex=datagridview1.rows.Add()”可能有异常“System.InvalidOperationException:当控件链接到数据时,无法通过编程将行添加到集合Datagridview。”?
        DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();


        ArrayList list1 = new ArrayList(); //{ "C-C", "C-O", "Absence composant", "Mauvaise valeur", "Mauvais sens", "Mauvais composant" };
        list1.Add("C-C");
        list1.Add("C-O");


        combo.DataSource = list1;
        combo.HeaderText = "FaultCodeByOp";
        combo.DataPropertyName = "FaultCodeByOp";


        dataGridView1.Columns.AddRange(combo);