DataGridViewComboBoxColumn选择项目从S

DataGridViewComboBoxColumn选择项目从S

本文介绍了在DataGridViewComboBoxColumn选择项目从SQL加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这是的SqlDataAdapter ,我使用的填充 dataGridView1 但在此之前我填它,我加入 DataGridViewComboBoxColumn 到datagridview的所以我将能够在存储在SQL ComboboxColumn选择项目。但我不知道如何才能实现

This is SqlDataAdapter that I use for filling dataGridView1 but before I fill it I add DataGridViewComboBoxColumn to that datagridview So I will be able to select item in ComboboxColumn that is stored in SQL. But I'm not sure how it can be achieved.

 dtSelectPronajem = new DataTable();
            SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM PRONAJEM WHERE NA_CISLKU=@NA_CISLKU", conn);
            SDA.SelectCommand.Parameters.AddWithValue("@NA_CISLKU", VybraneCisku);
            SDA.Fill(dtSelectPronajem);
            dataGridView1.DataSource = dtSelectPronajem;

创建DataGridViewComboBoxColumn:

Creating DataGridViewComboBoxColumn:

private void EditDGV()
    {
        try
        {

            DataTable dtPlodiny = new DataTable();
                string sqlQuery = "SELECT PLODINA, CENAZAQ FROM PLODINY";
                using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
                {
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dtPlodiny);
                }

                cbColumn = new DataGridViewComboBoxColumn();
                cbColumn.DataSource = dtPlodiny; //Changed with DataTable

                //add next two rows
                cbColumn.DisplayMember = "PLODINA";
                cbColumn.ValueMember = "PLODINA"; //property from .Datasource you want use as Value - reference to DataPropertyName

                cbColumn.DropDownWidth = 100;
                dataGridView1.Columns.Add(cbColumn);
                cbColumn.HeaderText = "Položka";
                this.cbColumn.Name = "POLOZKAcb";

        }



我的问题是 - 如何可能SelectText在组合框是从SQL加载的每一行?应在组合框可以再选择SelectedText值是在一个名为SQL列 PLO​​DINA

感谢您的时间这么多。

推荐答案

的DataGridViewColumn 必须加入 DataGridView的你之前设置 .DataSource

Your DataGridViewColumn must be added to DataGridView before you set .DataSource.

然后设置 .DataPropertyName 新列:

cbColumn.DataPropertyName = "PLODINA"

DataPropertyName 必须是从PRONAJEM字段的名称comboboxcolumn其中 .ValueMember = PRONAJEM.PLODINA(从数据表)将被选中

DataPropertyName must be name of the field from "PRONAJEM" table.
And then item of comboboxcolumn where .ValueMember = PRONAJEM.PLODINA(from DataTable) will be selected

这篇关于在DataGridViewComboBoxColumn选择项目从SQL加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:54