将数据从datagridview传递到表单C#

时间:2011-03-08 20:32:32

标签: c# sql datagridview

希望有人可以解决我的问题。我按照http://msdn.microsoft.com/en-us/library/ms171925(v=VS.100).aspx#Y3500中的教程进行操作,但无法使其正常工作。

我的代码如下:

   namespace CityCollectionCSharp
{
    public partial class frmSwitch : Form
    {
        public frmSwitch()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'newCityCollectionDataSet.ClientTable' table. You can move, or remove it, as needed.
            this.clientTableTableAdapter.Fill(this.newCityCollectionDataSet.ClientTable);
            // TODO: This line of code loads data into the 'newCityCollectionDataSet.PropertyInformation' table. You can move, or remove it, as needed.
            this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation);

        }

        private void propertyInformationDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            System.Data.DataRowView SelectedRowView;
            newCityCollectionDataSet.PropertyInformationRow SelectedRow;

            SelectedRowView = (System.Data.DataRowView)propertyInformationBindingSource.Current;
            SelectedRow = (newCityCollectionDataSet.PropertyInformationRow)SelectedRowView.Row;

            frmSummary SummaryForm = new frmSummary();
            SummaryForm.LoadCaseNumberKey(SelectedRow.CaseNumberKey);
            SummaryForm.Show();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            propertyInformationBindingSource.Filter = "ClientKey ='" + comboBox1.SelectedValue + "'";
        }




    }
}

这是第一种形式,现在是第二种形式:

    namespace CityCollectionCSharp
{
    public partial class frmSummary : Form
    {
        public frmSummary()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'newCityCollectionDataSet.PropertyInformation' table. You can move, or remove it, as needed.
            this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation);

        }
        internal void LoadCaseNumberKey(String CaseNumber)
        {
            propertyInformationTableAdapter.FillByCaseNumberKey(newCityCollectionDataSet.PropertyInformation, CaseNumber);

        }
    }
}

我在propertyInfromationTableAdapter中设置了如下查询:

SELECT        CaseNumberKey, BRTNumber, ParcelNumber, Premises, ClientKey, ParcelNum, Registry, TaxAcctName, StreetCode, CoverDate, OrderDate, Assessment, TaxFrom, TaxTo, TaxOpen, WaterOpen, WaterAcct, WaterTo, WaterFrom, AssessedBeg, AssessedDim, SumNotes, Legal, TotalWater, TotalTax, Type, OPARec, OPADoc, OPADocNum, Recital, Num, Name, Direction, Unit, ProductKey, DateFinished, Finished, Paid, BD, BDPaid, Search, Exam
FROM            PropertyInformation
WHERE        (CaseNumberKey = @CaseNumberKey)

我无法弄明白为什么这不符合规定。当我点击一个记录时,它会传递表中的两个记录,并且总是在我拥有的方框中有第一个记录。当我离开bindingnavigator时,我才知道这一点。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

每当我有一个问题上网工作时,它归结为我这样做/执行错误,误解了某些东西是如何工作的,因此无论如何它会破坏,或者代码来自第三方它可能无法启动或您发现错误。

MSDN代码不太可能出错(但并非不可能),所以我建议在你生成的代码中加入断点,如果不能启发那么我会创建一个全新的项目,逐字复制代码意思是你看到你哪里出错了,或者如果你得到了它的工作,然后慢慢复制你看到它什么时候看到它。

答案 1 :(得分:1)

试试这个,

Load to Datagridview using sqldatareader

DataBinding between DataSet and DataGridView in C#

How to: Bind Data to the Windows Forms DataGridView Control

您也可以使用sqlDatareader,sqlDataAdapter以编程方式加载Datagridview ..

此致