korrowan
korrowan

Reputation: 583

Passing Data from datagridview to form C#

Hoping someone can shed some light on my problem. I followed the tutorial found here http://msdn.microsoft.com/en-us/library/ms171925(v=VS.100).aspx#Y3500 and cannot get this to work.

My code is as follows:

   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 + "'";
        }




    }
}

That is for the first form and now the second form:

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

        }
    }
}

I have the query set up as follows in the 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)

I cannot figure out for the life of me why this does not work as prescribed. When I click on a record it passes both records in the table and always has the first one in the boxes I have. I only know this as I left the bindingnavigator. Any help would be much appreciated.

Upvotes: 1

Views: 1317

Answers (2)

Paul C
Paul C

Reputation: 4776

Whenever I have an issue getting to work off the net it boils down to me doing it / implementing it wrong, a misunderstanding of how something works and therefore it is going to break anyway or if the code is from a third party then it may not work to start or you find a bug.

It is unlikely the MSDN code is wrong (but not impossible) so I would suggest breakpoints galore in the code you have produced and if that doesn't enlighten then I would create a brand new project copying the code word for word may mean you see where you went wrong or if you get that working then slowly copy in what you have to see when it breaks.

Upvotes: 1

Crimsonland
Crimsonland

Reputation: 2204

Try this,

Load to Datagridview using sqldatareader

DataBinding between DataSet and DataGridView in C#

How to: Bind Data to the Windows Forms DataGridView Control

You can also use sqlDatareader,sqlDataAdapter to load on Datagridview Programatically..

Regards

Upvotes: 1

Related Questions