DataGridViewComboBoxColumn为SQL中的每一行保存SelectedText

时间:2013-11-12 11:11:49

标签: c# sql sql-server datagridview

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

谢谢大家的时间

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我认为,因为您没有为.ValueMember设置DataGridViewComboBoxColumn属性... ... 你需要设置
    .ValueMember - 从DataSource的属性设置Cell的值         此值将使用Column.DataPropertyName的值引用     .DisplayMember - 此值将用于下拉单元格中的diplay文本

DataTable中尝试使用DataSource作为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始终使用相同的列,则 如果你使用预定义的列(通过Designer创建/添加),我会很容易想到。然后,您可以为每个列设置名称,并在代码中将其用作对象:
this.MyColumnPolozka.Name...
例如......