【问题标题】:Passing Data from datagridview to form C#将数据从 datagridview 传递到表单 C#
【发布时间】:2011-03-08 20:32:31
【问题描述】:

希望有人能对我的问题有所了解。我按照这里找到的教程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)

我终其一生都无法弄清楚为什么这不能按规定工作。当我单击一条记录时,它会传递表中的两条记录,并且总是在我拥有的框中包含第一个记录。我只在离开绑定导航器时才知道这一点。任何帮助将不胜感激。

【问题讨论】:

    标签: c# sql datagridview


    【解决方案1】:

    每当我在网上工作时遇到问题,归结为我做错了/实施错误​​,误解了某些东西的工作原理,因此无论如何它都会崩溃,或者代码是否来自第三方它可能无法启动或您发现错误。

    MSDN 代码不太可能是错误的(但并非不可能),所以我建议在您生成的代码中设置大量断点,如果这没有启发,那么我将创建一个全新的项目,逐字复制代码可能意思是你看到哪里出了问题,或者如果你得到了那个工作,然后慢慢地复制你必须看到的东西,当它坏了。

    【讨论】:

    • 我知道这笔交易是什么。有一行代码填充了自动插入的视图并覆盖了查询。我删除了类似于 this.propertyInformationTableAdapter.Fill(this.newCityCollectionDataSet.PropertyInformation); 的代码行它解决了这个问题!
    【解决方案2】:

    试试这个,

    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..

    问候

    【讨论】:

    • 以编程方式加载它是填充数据网格视图和传递参数的更好方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多