DataBindings不起作用 [英] DataBindings Not Working

查看:73
本文介绍了DataBindings不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows 7企业版下运行的VS 2012中,我试图在表单上的数据集中显示数据。我已使用数据绑定将表单上的控件绑定到dataTable。表单正在工作并在dataTable中显示记录,但此后一直停止工作。
在调试模式下,我可以看到  dataTable 具有从我的存储过程返回的记录,但这些记录不再显示在表单上。不知道我做错了什么。谢谢。


使用System; 
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Data.SqlClient;
使用System.Globalization;
使用System.Diagnostics;
使用System.Collections;

名称空间MSP_Outreach
{
公共部分类MSPOutreach:表格
{
SqlDataAdapter da;
DataTable dataTable;
DataSet cobNonHorizo​​n;
CurrencyManager currManager;
布尔bolDUlete;
布尔bolConfirm;
布尔bolEdit;

public MSPOutreach()
{
InitializeComponent();
}

private void btnShow_Click(object sender,EventArgs e)
{
GetCOBNonHorizo​​nRecsForGrp();

/ *启用表格上的对象* /
btnConfirm.Enabled = true;
btnEdit.Enabled = true;
btnDelete.Enabled = true;
btnSave.Enabled = true;
btnNext.Enabled = true;
btnPrevious.Enabled = true;

bolEdit = false;
bolDelete = false;
bolConfirm = false;

txtComments.Enabled = true;

// RefreshDataGrid();

}

private void GetCOBNonHorizo​​nRecsForGrp()
{
Int32 rowsAffected;


string GroupName = cboGroups.Text

//填充dataTable
SqlCommand cmd1 = new SqlCommand(" Insurer_Select",sqlConnection1);
cmd1.Parameters.Clear();
cmd1.Parameters.Add(new SqlParameter(" @ InsurerGroup",GroupName));
cmd1.CommandType = CommandType.StoredProcedure;

// cobNonHorizo​​n = new DataSet();
da = new SqlDataAdapter(cmd1);
this.cobNonHorizo​​n.Clear();
da.Fill(cobNonHorizo​​n," dataTable");
sqlConnection1.Close();

DataTable tbl = cobNonHorizo​​n.Tables [0];

txtPosition.Text =" 1 of of + tbl.Rows.Count.ToString();


currManager =(CurrencyManager)this.BindingContext [cobNonHorizo​​n.Tables [" dataTable"]];

/ *启用状态选择按钮* /
btnEdit.Enabled = true;
btnConfirm.Enabled = true;
btnDelete.Enabled = true;

/ * DetermineStatus(); * /


}

private void MSPOutreach_Load(object sender,EventArgs e)
{
cboGroups.Items.AddRange(GetGroups( ));

//创建新的dataTable
dataTable = new DataTable(" dataTable");

/ *将列添加到datatable * /
dataTable.Columns.Add(" relHICN",typeof(String));
dataTable.Columns.Add(" relCCID",typeof(String));
dataTable.Columns.Add(" relSubscriberFirst",typeof(String));
dataTable.Columns.Add(" relSubscriberLast",typeof(String));
dataTable.Columns.Add(" relRelation",typeof(String));
dataTable.Columns.Add(" relMSPEffDate",typeof(String));
dataTable.Columns.Add(" relMSPTermDate",typeof(String));
dataTable.Columns.Add(" relMSPCode",typeof(String));
dataTable.Columns.Add(" relInsurerName",typeof(String));
dataTable.Columns.Add(" relInsurerAddress1",typeof(String));
dataTable.Columns.Add(" relInsurerCity",typeof(String));
dataTable.Columns.Add(" relInsurerSt",typeof(String));
dataTable.Columns.Add(" relInsurerZip",typeof(String));
dataTable.Columns.Add(" relPolicyNumber",typeof(String));
dataTable.Columns.Add(" relInsurerType",typeof(String));
dataTable.Columns.Add(" relSourceCode",typeof(String));
dataTable.Columns.Add(" relInsGroup",typeof(String));
dataTable.Columns.Add(" relInsurerGroupName",typeof(String));
dataTable.Columns.Add(" relOccuranceStatus",typeof(String));
dataTable.Columns.Add(" relActSource",typeof(String));
dataTable.Columns.Add(" relActMSPEffDate",typeof(String));
dataTable.Columns.Add(" relActMSPtermDate",typeof(String));
dataTable.Columns.Add(" relActMSPStatus",typeof(String));
dataTable.Columns.Add(" relIKAEffDate",typeof(String));
dataTable.Columns.Add(" relIKATermDate",typeof(String));
dataTable.Columns.Add(" relActInsurerName",typeof(String));
dataTable.Columns.Add(" relActRelation",typeof(String));
dataTable.Columns.Add(" relActInsurerAddress1",typeof(String));
dataTable.Columns.Add(" relActMSPCode",typeof(String));
dataTable.Columns.Add(" relActPolicyNumber",typeof(String));
dataTable.Columns.Add(" relActInsGroup",typeof(String));
dataTable.Columns.Add(" relActInsurerGroupName",typeof(String));
dataTable.Columns.Add(" relActInsurerType",typeof(String));
dataTable.Columns.Add(" relComment",typeof(String));
dataTable.Columns.Add(" relTransActionCd",typeof(String));

/ *为dataTable设置数据绑定* /
txtHICN.DataBindings.Add(" Text",dataTable," relHICN");
txtCCID.DataBindings.Add(" Text",dataTable," relCCID");
txtFN.DataBindings.Add(" Text",dataTable," relSubscriberFirst");
txtLN.DataBindings.Add(" Text",dataTable," relSubscriberLast");
txtPatRel.DataBindings.Add(" Text",dataTable," relRelation");
txtMSPEffDt.DataBindings.Add(" Text",dataTable," relMSPEffDate");
txtMSPTermDt.DataBindings.Add(" Text",dataTable," relMSPTermDate");
txtMSPCode.DataBindings.Add(" Text",dataTable," relMSPCode");
txtInsurerName.DataBindings.Add(" Text",dataTable," relInsurerName");
txtInsurerAddress.DataBindings.Add(" Text",dataTable," relInsurerAddress1");
txtInsurerCity.DataBindings.Add(" Text",dataTable," relInsurerCity");
txtInsurerSt.DataBindings.Add(" Text",dataTable," relInsurerSt");
txtInsurerZip.DataBindings.Add(" Text",dataTable," relInsurerZip");
txtPolicyNum.DataBindings.Add(" Text",dataTable," relPolicyNumber");
txtInsurerType.DataBindings.Add(" Text",dataTable," relInsurerType");
txtSourceCode.DataBindings.Add(" Text",dataTable," relSourceCode");
txtInsurerGrp.DataBindings.Add(" Text",dataTable," relInsGroup");
txtInsurerGrpName.DataBindings.Add(" Text",dataTable," relInsurerGroupName");
txtStatus.DataBindings.Add(" Text",dataTable," relOccuranceStatus");
txtSourceCd.DataBindings.Add(" Text",dataTable," relActSource");
txtActEffDt.DataBindings.Add(" Text",dataTable," relActMSPEffDate");
txtActTermDt.DataBindings.Add(" Text",dataTable," relActMSPtermDate");
txtMSPStatus.DataBindings.Add(" Text",dataTable," relActMSPStatus");
txtIKAEff.DataBindings.Add(" Text",dataTable," relIKAEffDate");
txtIKATerm.DataBindings.Add(" Text",dataTable," relIKATermDate");
txtActInsurerName.DataBindings.Add(" Text",dataTable," relActInsurerName");
txtActPatientRel.DataBindings.Add(" Text",dataTable," relActRelation");
txtActInsurerAddress1.DataBindings.Add(" Text",dataTable," relActInsurerAddress1");
txtActMSPCode.DataBindings.Add(" Text",dataTable," relActMSPCode");
txtActPolicyNum.DataBindings.Add(" Text",dataTable," relActPolicyNumber");
txtActInsurerGrp.DataBindings.Add(" Text",dataTable," relActInsGroup");
txtActInsurerGrpName.DataBindings.Add(" Text",dataTable," relActInsurerGroupName");
txtActInsurerType.DataBindings.Add(" Text",dataTable," relActInsurerType");
txtComments.DataBindings.Add(" Text",dataTable," relComment");
txtTransActionCd.DataBindings.Add(" Text",dataTable," relTransActionCd");

// DataTable dtIKASpans = new DataTable();
cobNonHorizo​​n = new DataSet();
}

public string [] GetGroups()
{
var groups = new List< string>();
sqlConnection1.Open();
SqlCommand cmd = new SqlCommand(" GetInsurerGroups",sqlConnection1);
cmd.CommandType = CommandType.StoredProcedure;
var reader = cmd.ExecuteReader();
while(reader.Read())
{
groups.Add(reader.GetString(0));
}

sqlConnection1.Close();

返回groups.ToArray();
}
}
}

解决方案

通过查看代码而无法运行它,我不得不想知道为什么要创建DataTable然后添加列然后进行数据绑定。相反,当从存储过程中读取数据时,结果应该通过DataTable返回,例如datatable.Load(cmd.ExecuteReader())
用数据和当然列填充数据表。


还想知道为什么要在load事件中创建新数据集


In VS 2012 running under Windows 7 Enterprise I am trying to display data in a dataset on a form. I have bound the controls on the form to the dataTable using databindings. The form was working and showed records in the dataTable, but has since stopped working. In debug mode I can see that the dataTable has records returned from my stored procedure but those records are no longer showing on the form. Not sure what I'm doing wrong. Thank you.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Globalization;
using System.Diagnostics;
using System.Collections;

namespace MSP_Outreach
{
    public partial class MSPOutreach : Form
    {
        SqlDataAdapter da;
        DataTable dataTable;
        DataSet cobNonHorizon;
        CurrencyManager currManager;
        Boolean bolDelete;
        Boolean bolConfirm;
        Boolean bolEdit;

        public MSPOutreach()
        {
            InitializeComponent();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            GetCOBNonHorizonRecsForGrp();

            /* Enable objects on form */
            btnConfirm.Enabled = true;
            btnEdit.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = true;
            btnNext.Enabled = true;
            btnPrevious.Enabled = true;

            bolEdit = false;
            bolDelete = false;
            bolConfirm = false;

            txtComments.Enabled = true;

            //RefreshDataGrid();

        }

        private void GetCOBNonHorizonRecsForGrp()
        {
            Int32 rowsAffected;

       
            string GroupName = cboGroups.Text

            //Populate dataTable
            SqlCommand cmd1 = new SqlCommand("Insurer_Select", sqlConnection1);
            cmd1.Parameters.Clear();
            cmd1.Parameters.Add(new SqlParameter("@InsurerGroup", GroupName));
            cmd1.CommandType = CommandType.StoredProcedure;

            //cobNonHorizon = new DataSet();
            da = new SqlDataAdapter(cmd1);
            this.cobNonHorizon.Clear();
            da.Fill(cobNonHorizon, "dataTable");
            sqlConnection1.Close();

            DataTable tbl = cobNonHorizon.Tables[0];

            txtPosition.Text = "1 of " + tbl.Rows.Count.ToString();


            currManager = (CurrencyManager)this.BindingContext[cobNonHorizon.Tables["dataTable"]];

            /* Enable status selection buttons */
            btnEdit.Enabled = true;
            btnConfirm.Enabled = true;
            btnDelete.Enabled = true;

            /* DetermineStatus(); */


        }

        private void MSPOutreach_Load(object sender, EventArgs e)
        {
            cboGroups.Items.AddRange(GetGroups());

            //Create new dataTable
            dataTable = new DataTable("dataTable");

            /* Add columns to datatable */
            dataTable.Columns.Add("relHICN", typeof(String));
            dataTable.Columns.Add("relCCID", typeof(String));
            dataTable.Columns.Add("relSubscriberFirst", typeof(String));
            dataTable.Columns.Add("relSubscriberLast", typeof(String));
            dataTable.Columns.Add("relRelation", typeof(String));
            dataTable.Columns.Add("relMSPEffDate", typeof(String));
            dataTable.Columns.Add("relMSPTermDate", typeof(String));
            dataTable.Columns.Add("relMSPCode", typeof(String));
            dataTable.Columns.Add("relInsurerName", typeof(String));
            dataTable.Columns.Add("relInsurerAddress1", typeof(String));
            dataTable.Columns.Add("relInsurerCity", typeof(String));
            dataTable.Columns.Add("relInsurerSt", typeof(String));
            dataTable.Columns.Add("relInsurerZip", typeof(String));
            dataTable.Columns.Add("relPolicyNumber", typeof(String));
            dataTable.Columns.Add("relInsurerType", typeof(String));
            dataTable.Columns.Add("relSourceCode", typeof(String));
            dataTable.Columns.Add("relInsGroup", typeof(String));
            dataTable.Columns.Add("relInsurerGroupName", typeof(String));
            dataTable.Columns.Add("relOccuranceStatus", typeof(String));
            dataTable.Columns.Add("relActSource", typeof(String));
            dataTable.Columns.Add("relActMSPEffDate", typeof(String));
            dataTable.Columns.Add("relActMSPtermDate", typeof(String));
            dataTable.Columns.Add("relActMSPStatus", typeof(String));
            dataTable.Columns.Add("relIKAEffDate", typeof(String));
            dataTable.Columns.Add("relIKATermDate", typeof(String));
            dataTable.Columns.Add("relActInsurerName", typeof(String));
            dataTable.Columns.Add("relActRelation", typeof(String));
            dataTable.Columns.Add("relActInsurerAddress1", typeof(String));
            dataTable.Columns.Add("relActMSPCode", typeof(String));
            dataTable.Columns.Add("relActPolicyNumber", typeof(String));
            dataTable.Columns.Add("relActInsGroup", typeof(String));
            dataTable.Columns.Add("relActInsurerGroupName", typeof(String));
            dataTable.Columns.Add("relActInsurerType", typeof(String));
            dataTable.Columns.Add("relComment", typeof(String));
            dataTable.Columns.Add("relTransActionCd", typeof(String));
 
            /* Set data bindings for dataTable */
            txtHICN.DataBindings.Add("Text", dataTable, "relHICN");
            txtCCID.DataBindings.Add("Text", dataTable, "relCCID");
            txtFN.DataBindings.Add("Text", dataTable, "relSubscriberFirst");
            txtLN.DataBindings.Add("Text", dataTable, "relSubscriberLast");
            txtPatRel.DataBindings.Add("Text", dataTable, "relRelation");
            txtMSPEffDt.DataBindings.Add("Text", dataTable, "relMSPEffDate");
            txtMSPTermDt.DataBindings.Add("Text", dataTable, "relMSPTermDate");
            txtMSPCode.DataBindings.Add("Text", dataTable, "relMSPCode");
            txtInsurerName.DataBindings.Add("Text", dataTable, "relInsurerName");
            txtInsurerAddress.DataBindings.Add("Text", dataTable, "relInsurerAddress1");
            txtInsurerCity.DataBindings.Add("Text", dataTable, "relInsurerCity");
            txtInsurerSt.DataBindings.Add("Text", dataTable, "relInsurerSt");
            txtInsurerZip.DataBindings.Add("Text", dataTable, "relInsurerZip");
            txtPolicyNum.DataBindings.Add("Text", dataTable, "relPolicyNumber");
            txtInsurerType.DataBindings.Add("Text", dataTable, "relInsurerType");
            txtSourceCode.DataBindings.Add("Text", dataTable, "relSourceCode");
            txtInsurerGrp.DataBindings.Add("Text", dataTable, "relInsGroup");
            txtInsurerGrpName.DataBindings.Add("Text", dataTable, "relInsurerGroupName");
            txtStatus.DataBindings.Add("Text", dataTable, "relOccuranceStatus");
            txtSourceCd.DataBindings.Add("Text", dataTable, "relActSource");
            txtActEffDt.DataBindings.Add("Text", dataTable, "relActMSPEffDate");
            txtActTermDt.DataBindings.Add("Text", dataTable, "relActMSPtermDate");
            txtMSPStatus.DataBindings.Add("Text", dataTable, "relActMSPStatus");
            txtIKAEff.DataBindings.Add("Text", dataTable, "relIKAEffDate");
            txtIKATerm.DataBindings.Add("Text", dataTable, "relIKATermDate");
            txtActInsurerName.DataBindings.Add("Text", dataTable, "relActInsurerName");
            txtActPatientRel.DataBindings.Add("Text", dataTable, "relActRelation");
            txtActInsurerAddress1.DataBindings.Add("Text", dataTable, "relActInsurerAddress1");
            txtActMSPCode.DataBindings.Add("Text", dataTable, "relActMSPCode");
            txtActPolicyNum.DataBindings.Add("Text", dataTable, "relActPolicyNumber");
            txtActInsurerGrp.DataBindings.Add("Text", dataTable, "relActInsGroup");
            txtActInsurerGrpName.DataBindings.Add("Text", dataTable, "relActInsurerGroupName");
            txtActInsurerType.DataBindings.Add("Text", dataTable, "relActInsurerType");
            txtComments.DataBindings.Add("Text", dataTable, "relComment");
            txtTransActionCd.DataBindings.Add("Text", dataTable, "relTransActionCd");
 
            //DataTable dtIKASpans = new DataTable();
            cobNonHorizon = new DataSet();
        }

        public string[] GetGroups()
        {
            var groups = new List<string>();
            sqlConnection1.Open();
            SqlCommand cmd = new SqlCommand("GetInsurerGroups", sqlConnection1);
            cmd.CommandType = CommandType.StoredProcedure;
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                groups.Add(reader.GetString(0));
            }

            sqlConnection1.Close();

            return groups.ToArray();
        }
    }
}

解决方案

By looking at your code without being able to run it, I have to wonder why you create a DataTable then add columns then do data binding. Instead when reading data from a stored procedure the results should come back via a DataTable e.g. datatable.Load(cmd.ExecuteReader()) which populates the Datatable with data and of course columns.

Also wonder why you are creating a new data set in the load event


这篇关于DataBindings不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆