DataRowView而不是value

时间:2017-10-24 15:21:01

标签: c# sql combobox datarowview

每当我尝试运行我的应用程序时都会收到错误,因为我没有获得comboBox.SelectedValue的实际值,而是获得DataRowView项。

以下是我收到错误的代码:

private void InitDataGridView()
{
    query = "SELECT p.name, p.age FROM Person p INNER JOIN Class c ON p.idC=c.idC WHERE p.id=" 
            + comboBoxClass.SelectedValue;
    command = new SqlCommand(query, connection);
    adapter = new SqlDataAdapter(command);
    datatable = new DataTable();
    adapter.Fill(datatable);
    dataGridViewStudents.DataSource = datatable;
}

comboBoxClass.SelectedValue应该返回"idC",因为我设置了DataSourceDisplayMember和(ValueMember -> idC)。

idC是主键(int)。

ComboBox设置:

comboBoxClass.DataSource = datatable;
comboBoxClass.DisplayMember = "className";
comboBoxClass.ValueMember = "idC";

2 个答案:

答案 0 :(得分:0)

不绑定或不正确地绑定ValueMember可以产生您描述的确切效果。正如您在下面的代码片段中看到的那样。

ComboBox的初始化上加上一些断点,并弄清楚为什么ValueMember不是你需要的。然后你的DataGridView应该正确填充。

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace BindingToDataTable_46914296
{


    public partial class Form1 : Form
    {
        ComboBox combob = new ComboBox();
        ComboBox combobFail = new ComboBox();
        TextBox txtbx = new TextBox();
        public Form1()
        {
            InitializeComponent();
            InitComboBox();
            InitComboBoxFail();
            InitTxtBx();
        }

        private void InitTxtBx()
        {
            txtbx.Location = new Point(5, 30);
            txtbx.Width = this.Width - 10;
            this.Controls.Add(txtbx);
        }

        /// <summary>
        /// This version works, the proper selected value shows up in the textbox
        /// </summary>
        private void InitComboBox()
        {
            combob.Location = new Point(5,5);
            this.Controls.Add(combob);

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Col1", typeof(string)));
            dt.Columns.Add(new DataColumn("Col2", typeof(string)));
            dt.Columns.Add(new DataColumn("Col3", typeof(string)));
            dt.Columns.Add(new DataColumn("Col4", typeof(Int32)));
            dt.Rows.Add("blah1", "bleh", "bloh", 1);
            dt.Rows.Add("blah2", "bleh", "bloh", 2);
            dt.Rows.Add("blah3", "bleh", "bloh", 3);
            dt.Rows.Add("blah4", "bleh", "bloh", 4);
            combob.DataSource = dt;
            combob.DisplayMember = "Col1";
            combob.ValueMember = "Col4";

            combob.SelectedValueChanged += Combob_SelectedValueChanged;
        }


        /// <summary>
        /// This version DOES NOT work, a DataRowView item appears in the textbox when the selection changes
        /// </summary>
        private void InitComboBoxFail()
        {
            combobFail.Location = new Point(combob.Location.X + combob.Width + 5, 5);
            this.Controls.Add(combobFail);

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Col1", typeof(string)));
            dt.Columns.Add(new DataColumn("Col2", typeof(string)));
            dt.Columns.Add(new DataColumn("Col3", typeof(string)));
            dt.Columns.Add(new DataColumn("Col4", typeof(Int32)));
            dt.Rows.Add("blah1", "bleh", "bloh", 1);
            dt.Rows.Add("blah2", "bleh", "bloh", 2);
            dt.Rows.Add("blah3", "bleh", "bloh", 3);
            dt.Rows.Add("blah4", "bleh", "bloh", 4);
            combobFail.DataSource = dt;
            combobFail.DisplayMember = "Col1";
            //only difference is I am not binding ValueMember

            combobFail.SelectedValueChanged += Combob_SelectedValueChanged;
        }

        private void Combob_SelectedValueChanged(object sender, EventArgs e)
        {
            txtbx.Text = ((ComboBox)sender).SelectedValue.ToString();
        }
    }
}

答案 1 :(得分:0)

好的,我发现我的代码出了什么问题。它既不是<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="myIcon" style = "width:100px"><svg viewBox="0 0 100 100" focusable="false"><g class="bg"><circle cx="50" cy="50" r="45.56" style="stroke-width:4.44;stroke-linecap:butt;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0"></circle></g><g class="fg" transform="translate(50,50) scale(0.5) translate(-50,-50)"><g transform="matrix(0.19920319,0,0,0.19920319,2.5095651e-4,-1.38e-6)"> <g> <path d="m 478.482,448.601 -101.6,-101.599 c 8.089,-15.262 12.339,-32.29 12.339,-49.717 0,-35.689 -18.182,-69.164 -47.804,-88.743 V 92.53 c 0,-2.652 -1.054,-5.196 -2.929,-7.071 L 255.959,2.929 C 254.083,1.054 251.54,0 248.888,0 H 24.371 c -5.522,0 -10,4.477 -10,10 v 427.735 c 0,5.523 4.478,10 10,10 h 307.046 c 5.522,0 10,-4.477 10,-10 v -37.722 l 92.825,92.825 c 5.908,5.908 13.764,9.162 22.119,9.162 8.356,0 16.212,-3.254 22.12,-9.162 12.197,-12.196 12.197,-32.041 0.001,-44.237 z M 307.276,82.53 H 258.889 V 34.143 l 24.193,24.193 24.194,24.194 z m 14.141,313.847 v 31.358 H 34.371 V 20 h 204.517 v 72.53 c 0,5.523 4.478,10 10,10 h 72.529 v 95.662 l 0.009,0.014 c -12.214,-4.741 -25.275,-7.215 -38.499,-7.215 -58.61,0 -106.294,47.683 -106.294,106.293 0,58.61 47.684,106.293 106.294,106.293 13.224,0 26.285,-2.474 38.499,-7.215 l -0.009,0.015 z m 7.531,-26.086 c -0.848,0.536 -1.706,1.057 -2.574,1.563 -13.131,7.67 -28.154,11.724 -43.446,11.724 -47.583,0 -86.294,-38.711 -86.294,-86.293 0,-47.582 38.711,-86.293 86.294,-86.293 15.291,0 30.315,4.054 43.447,11.724 26.428,15.435 42.846,44.008 42.846,74.569 0,16.35 -4.595,32.264 -13.289,46.022 -6.835,10.818 -16.166,20.148 -26.984,26.984 z m 135.391,108.405 c -2.131,2.131 -4.964,3.304 -7.978,3.304 -3.014,0 -5.847,-1.173 -7.977,-3.304 l -98.706,-98.706 -0.008,-0.001 c 5.856,-4.74 11.221,-10.104 15.961,-15.96 l 0.001,0.008 98.707,98.707 c 4.398,4.398 4.398,11.554 0,15.952 z"></path> <path d="m 246.838,238.403 c -20.641,12.674 -32.964,34.686 -32.964,58.882 0,5.523 4.478,10 10,10 5.522,0 10,-4.477 10,-10 0,-17.19 8.759,-32.83 23.429,-41.838 4.707,-2.89 6.179,-9.048 3.289,-13.754 -2.89,-4.707 -9.048,-6.18 -13.754,-3.29 z"></path> <path d="m 317.708,237.624 c -10.52,-6.145 -22.547,-9.392 -34.781,-9.392 -5.522,0 -10,4.477 -10,10 0,5.523 4.478,10 10,10 8.693,0 17.232,2.304 24.693,6.662 1.586,0.926 3.321,1.367 5.034,1.367 3.438,0 6.785,-1.775 8.645,-4.958 2.786,-4.77 1.178,-10.894 -3.591,-13.679 z"></path> </g> </g></g></svg></div> <br> <button id = "btn">animate</button>初始化也不是comboBox,但写下错误查询完全是我的错。

我感谢大家帮助我的努力。