0

这是T-SQL用于将数据插入datagridview其中SQL Table,但问题是 datagridview 的列之一是DataGridViewComboBoxColumn,我需要为每一行保存组合框中的特定选定项目。请问可以怎么做?

    public DataGridViewComboBoxColumn cbColumn;
conn.Open();
        SqlTransaction sqlTrans = conn.BeginTransaction();
        try
        {

            string delCmdTxt = "DELETE FROM PRONAJEM WHERE NA_CISLKU=@NA_CISLKU";
            SqlCommand cmdDel = conn.CreateCommand();
            cmdDel.Parameters.AddWithValue("@NA_CISLKU",VybraneCisku);
            cmdDel.CommandText = delCmdTxt;
            cmdDel.Transaction = sqlTrans;
            cmdDel.ExecuteNonQuery();

            string insert_sql = "INSERT INTO PRONAJEM(DATUM,PLODINA,CENAMJ,MNOZSTVIMJ,PRIKAZ,NA_ZUSTA,NA_CISLKU,RODNECISLO)VALUES" +
                "(@DATUM,@PLODINA,@CENAMJ,@MNOZSTVIMJ,@PRIKAZ,@NA_ZUSTA,@NA_CISLKU,@RODNECISLO)";
            using (SqlCommand sqlcom = conn.CreateCommand())
            {
                sqlcom.CommandText = insert_sql;
                sqlcom.Transaction = sqlTrans;

                sqlcom.Parameters.Add("@DATUM", SqlDbType.Date); //Replace with whatever the correct datatypes are
                sqlcom.Parameters.Add("@PLODINA", SqlDbType.NVarChar);
                sqlcom.Parameters.Add("@CENAMJ", SqlDbType.Decimal);
                sqlcom.Parameters.Add("@MNOZSTVIMJ", SqlDbType.Decimal);
                sqlcom.Parameters.Add("@PRIKAZ", SqlDbType.Date);
                sqlcom.Parameters.Add("@NA_ZUSTA", SqlDbType.Decimal);
                sqlcom.Parameters.Add("@NA_CISLKU", SqlDbType.NVarChar);
                sqlcom.Parameters.Add("@RODNECISLO", SqlDbType.NVarChar);

                var validRows = dataGridView1.Rows.Cast<DataGridViewRow>()
            .Where(row => row.Cells["DATUM"].Value != null);

                foreach (DataGridViewRow row in validRows)
                {
                    sqlcom.Parameters[0].Value = row.Cells["DATUM"].Value;
                    sqlcom.Parameters[1].Value = row.Cells["POLOZKAcb"].Value;
                    sqlcom.Parameters[2].Value = row.Cells["CENAMJ"].Value;
                    sqlcom.Parameters[3].Value = row.Cells["MNOZSTVIMJ"].Value;
                    sqlcom.Parameters[4].Value = row.Cells["PRIKAZ"].Value;
                    sqlcom.Parameters[5].Value = row.Cells["NA_ZUSTA"].Value;
                    sqlcom.Parameters[6].Value = VybraneCisku;
                    sqlcom.Parameters[7].Value = VybraneRodneCislo;

                    sqlcom.ExecuteNonQuery();
                }
                sqlcom.Dispose();
            }
            sqlTrans.Commit();

这是 DataGridViewComboBoxColumn 的创建:

 string query = "SELECT * FROM PLODINY ";
            SqlCommand newcom = new SqlCommand(query, conn);
            conn.Open();
            SqlDataReader reader= newcom .ExecuteReader();

            List<string> listPlodiny = new List<string>();
            while (reader.Read())
            {

                listPlodiny.Add(reader.GetString(reader.GetOrdinal("PLODINA")) + "-" + Decimal.Parse(reader["CENAZAQ"].ToString()).ToString());
                for (int i = 0; i <= listPlodiny.Count() - 1; i++)
                {

                }
            }
            cbColumn = new DataGridViewComboBoxColumn();
            cbColumn.DataSource = listPlodiny;
            cbColumn.DropDownWidth = 100;
            dataGridView1.Columns.Add(cbColumn);
            cbColumn.DisplayIndex = 3;
            cbColumn.HeaderText = "Položka";
            cbColumn.DataPropertyName = "POLOZKAcb";

谢谢大家的时间

4

2 回答 2

1

如果您没有从中获得正确的值,请row.Cells(0).Value尝试将其转换为下拉列表

于 2013-11-12T11:33:44.553 回答
1

我认为因为您没有为...设置.ValueMember属性,所以 您需要从 DataSource 的属性中设置 - 为 Cell 设置值。 该值将被 Column.DataPropertyName 的值引用。- 此值将用于下拉单元格中的显示文本 DataGridViewComboBoxColumn

.ValueMember

.DisplayMember

尝试下一个(更新为使用DataTableas DataSourcein DataGridViewComboBoxColumn

DataTable dtPlodiny;
using(SqlConnection sqlConn = new SqlConnection(conn))//conn - your connection string
{
    string sqlQuery = @"SELECT ID, Description FROM PLODINY"; //better practice use only fields you need
    using(SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
    {
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dtPlodiny);
    }
}
cbColumn = new DataGridViewComboBoxColumn();
cbColumn.Name = "POLOZKAcb" 
cbColumn.DataSource = dtPlodiny; //Changed with DataTable

//add next two rows
cbColumn.DisplayMember = "Description" //property from .Datasource you want to show for user
cbColumn.ValueMember = "ID" //property from .Datasource you want use as Value - reference to DataPropertyName

cbColumn.DropDownWidth = 100;
dataGridView1.Columns.Add(cbColumn);
cbColumn.DisplayIndex = 3;
cbColumn.HeaderText = "Položka";
cbColumn.DataPropertyName = "POLOZKAcb";

如果您的 datagridview 始终使用相同的列,
那么如果您使用预定义的列(通过设计器创建/添加),我认为很容易。然后,您可以为每一列设置一个名称,并在代码中将其用作对象:
this.MyColumnPolozka.Name...
例如...

于 2013-11-12T11:55:23.690 回答