首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataRowView而不是值

DataRowView而不是值
EN

Stack Overflow用户
提问于 2017-10-24 15:21:00
回答 2查看 133关注 0票数 1

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

下面是我得到错误的代码:

代码语言:javascript
复制
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设置:

代码语言:javascript
复制
comboBoxClass.DataSource = datatable;
comboBoxClass.DisplayMember = "className";
comboBoxClass.ValueMember = "idC";
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-10-25 12:42:17

好吧,我想出了我的代码出了什么问题。这既不是comboBox的初始化,也不是dataGridView的,但写错查询完全是我的错。

我感谢大家为帮助我而付出的努力。

票数 0
EN

Stack Overflow用户

发布于 2017-10-24 21:27:49

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

ComboBox的初始化上放置一些断点,并找出为什么ValueMember不是您所需要的。然后,您的DataGridView应该正确地填充。

代码语言:javascript
复制
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();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46914296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档