System.Data.DataRowView显示在winforms组合框中而不是实际数据中

时间:2015-03-27 14:57:49

标签: c# .net winforms combobox

加载组合框后,它显示System.Data.DataRowView而不是数据。我使用从存储过程返回的正确列名(TimeframeDesc和TimeframeCode)。

调试时收到“无法绑定到新显示成员”的消息。

        private bool loadTimeframeList(out string msg)
    {
        msg = "";
        string spName = "schemaExec.SelTimeframeCode";
        bool result = true;
        SqlCommand cmd = new SqlCommand(spName);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = _sqlConn;

        try
        {
            SqlDataAdapter rdr = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            rdr.Fill(dt);

            cbTimeframe.DisplayMember = "TimeframeDesc";
            cbTimeframe.ValueMember = "TimeframeCode";
            cbTimeframe.DataSource = dt;             
        }
        catch (Exception ex)
        {
            result = false;
            msg = "Could not load Timeframe Codes" + Environment.NewLine + ex.Message;
        }
        return result;
    }

3 个答案:

答案 0 :(得分:0)

不确定这是否会导致您的问题,但在填写表格之前,您不必使用cmd.ExecuteNonQuery();。只是:

DataTable dt = new DataTable();
using(SqlCommand cmd = new SqlCommand("schemaExec.SelTimeframeCode", _sqlConn))
{
    cmd.CommandType = CommandType.StoredProcedure;
    using(var da = new SqlDataAdapter(cmd))
        da.Fill(dt);
}
cbTimeframe.DisplayMember = "TimeframeDesc";
cbTimeframe.ValueMember = "TimeframeCode";
cbTimeframe.DataSource = dt;     

答案 1 :(得分:0)

在断点处,在Visual Studio中,使用放大镜打开数据表的调试查看器。或者表格中的foreach DataColumn会将列名添加到StringBuilder

答案 2 :(得分:0)

这不是你的情况,但有时候当你恢复订单时,如果数据源是在DisplayMemberValueMember属性之前设置的,则会发生这种情况。或者没有从以前的数据源中清除。

cbTimeframe.DataSource = dt;
cbTimeframe.DisplayMember = "TimeframeDesc";
cbTimeframe.ValueMember = "TimeframeCode";

或者

cbTimeframe.DataSource = ds1;
cbTimeframe.DisplayMember = "fn1";
cbTimeframe.ValueMember = "fn1";

cbTimeframe.DataSource = ds2; -> here you will get the problem (in the Event of index changed) if u didn't clean the DataSource used .
cbTimeframe.DisplayMember = "fn2";
cbTimeframe.ValueMember = "fn2";