【问题标题】:DataRowView instead of valueDataRowView 而不是值
【发布时间】:2018-04-05 11:21:57
【问题描述】:

每当我尝试运行我的应用程序时都会收到错误消息,因为我没有获得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";

【问题讨论】:

  • 你有一个 SQL 注入漏洞。
  • 你能说明你是如何设置你的ComboBox的吗?
  • 当然! comboBoxClass.DataSource = datatable; comboBoxClass.DisplayMember = "className"; comboBoxClass.ValueMember = "idC";
  • 我认为我们正在查看您的代码的错误部分。错误发生在您写的内容中,但核心问题似乎在comboBoxClass 中。让我们看一下初始化 comboBoxClass 的代码,这样我们就可以理解为什么您的 comboBoxClass.SelectedValueDataRowViewItem 而不是 stringint,就像您期望的那样。
  • 现在更改我的查询我终于可以看到 id,尽管当SqlDataAdapter 尝试填充 DataTable 时我仍然得到一个System.Data.SqlClient.SqlException。就在这里adapter.Fill(datatable)

标签: c# sql combobox datarowview


【解决方案1】:

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

感谢大家对我的帮助。

【讨论】:

  • 哪个查询?查询驱动是哪个控件?如果查询错误,而你使用查询来初始化控件,你不会认为你的初始化是错误的吗?不管怎样,很高兴你修好了。
  • 是的,实际上你是对的。查询是获取 classID 和 className 的简单选择,所以是的,它是初始化。
【解决方案2】:

不绑定或不正确绑定 ValueMember 可能会产生您所描述的确切效果。正如您在下面的 sn-p 中看到的那样。

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();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多