Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 692063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:36:42+00:00 2026-05-14T02:36:42+00:00

I have a gridview and, when a record is double-clicked, I want it to

  • 0

I have a gridview and, when a record is double-clicked, I want it to open up a new detail-view form for that particular record.

As an example, I have created a Customer class:

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Collections;

namespace SyncTest
{
#region Customer Collection

public class CustomerCollection : BindingListView<Customer>
{
    public CustomerCollection() : base()
    {
    }

    public CustomerCollection(List<Customer> customers) : base(customers)
    {
    }

    public CustomerCollection(DataTable dt)
    {
        foreach (DataRow oRow in dt.Rows)
        {
            Customer c = new Customer(oRow);
            this.Add(c);
        }
    }
}

#endregion

public class Customer : INotifyPropertyChanged, IEditableObject, IDataErrorInfo
{
    private string _CustomerID;
    private string _CompanyName;
    private string _ContactName;
    private string _ContactTitle;
    private string _OldCustomerID;
    private string _OldCompanyName;
    private string _OldContactName;
    private string _OldContactTitle; 
    private bool _Editing;
    private string _Error = string.Empty;
    private EntityStateEnum _EntityState;
    private Hashtable _PropErrors = new Hashtable();

    public event PropertyChangedEventHandler PropertyChanged;

    private void FirePropertyChangeNotification(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    public Customer()
    {
        this.EntityState = EntityStateEnum.Unchanged;
    }

    public Customer(DataRow dr)
    {
        //Populates the business object item from a data row
        this.CustomerID = dr["CustomerID"].ToString();
        this.CompanyName = dr["CompanyName"].ToString();
        this.ContactName = dr["ContactName"].ToString();
        this.ContactTitle = dr["ContactTitle"].ToString();

        this.EntityState = EntityStateEnum.Unchanged;
    }

    public string CustomerID
    {
        get
        {
            return _CustomerID;
        }
        set
        {
            _CustomerID = value;
            FirePropertyChangeNotification("CustomerID");
        }
    }

    public string CompanyName
    {
        get
        {
            return _CompanyName;
        }
        set
        {
            _CompanyName = value;
            FirePropertyChangeNotification("CompanyName");
        }
    }

    public string ContactName
    {
        get
        {
            return _ContactName;
        }
        set
        {
            _ContactName = value;
            FirePropertyChangeNotification("ContactName");
        }
    }

    public string ContactTitle
    {
        get
        {
            return _ContactTitle;
        }
        set
        {
            _ContactTitle = value;
            FirePropertyChangeNotification("ContactTitle");
        }
    }

    public Boolean IsDirty
    {
        get
        {
            return ((this.EntityState != EntityStateEnum.Unchanged) || (this.EntityState != EntityStateEnum.Deleted));
        }
    }

    public enum EntityStateEnum
    {
        Unchanged,
        Added,
        Deleted,
        Modified
    }

    void IEditableObject.BeginEdit()
    {
        if (!_Editing)
        {
            _OldCustomerID = _CustomerID;
            _OldCompanyName = _CompanyName;
            _OldContactName = _ContactName;
            _OldContactTitle = _ContactTitle;
        }
        this.EntityState = EntityStateEnum.Modified;
        _Editing = true;
    }

    void IEditableObject.CancelEdit()
    {
        if (_Editing)
        {
            _CustomerID = _OldCustomerID;
            _CompanyName = _OldCompanyName;
            _ContactName = _OldContactName;
            _ContactTitle = _OldContactTitle;
        }
        this.EntityState = EntityStateEnum.Unchanged;
        _Editing = false;
    }

    void IEditableObject.EndEdit()
    {
        _Editing = false;
    }

    public EntityStateEnum EntityState
    {
        get
        {
            return _EntityState;
        }
        set
        {
            _EntityState = value;
        }
    }

    string IDataErrorInfo.Error
    {
        get
        {
            return _Error;
        }
    }

    string IDataErrorInfo.this[string columnName]
    {
        get
        {
            return (string)_PropErrors[columnName];
        }
    }

    private void DataStateChanged(EntityStateEnum dataState, string propertyName)
    {
        //Raise the event
        if (PropertyChanged != null && propertyName != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        //If the state is deleted, mark it as deleted
        if (dataState == EntityStateEnum.Deleted)
        {
            this.EntityState = dataState;
        }
        if (this.EntityState == EntityStateEnum.Unchanged)
        {
            this.EntityState = dataState;
        }
    }
}

}

Here is my the code for the double-click event:

        private void customersDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        Customer oCustomer = (Customer)customersBindingSource.CurrencyManager.List[customersBindingSource.CurrencyManager.Position];
        CustomerForm oForm = new CustomerForm();
        oForm.NewCustomer = oCustomer;
        oForm.ShowDialog(this);

        oForm.Dispose();
        oForm = null;
    }

Unfortunately, when this code runs, I receive an InvalidCastException error stating “Unable to cast object to type ‘System.Data.DataRowView’ to type ‘SyncTest.Customer'”.

This error occurs on the very first line of that event:

Customer oCustomer = (Customer)customersBindingSource.CurrencyManager.List[customersBindingSource.CurrencyManager.Position];

What am I doing wrong?… and what can I do to fix this? Any help is greatly appreciated.

Thanks!


EDIT:

Here is how the data is populated:

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'northwindDataSet.Customers' table. You can move, or remove it, as needed.
        this.customersTableAdapter.Fill(this.northwindDataSet.Customers);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:36:42+00:00Added an answer on May 14, 2026 at 2:36 am

    It looks like your object is of type System.Data.DataRowView, not Customer. Your code returns Dataset instead of objects that you are expecting. You need to modify the code that returns your objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have gridview that I am using paging on. I want to pass along
I have a page that contains a Gridview showing a record from a db
I have record from sql database printing into gridview. I want to make another
I have a GridView control in an Asp.net application, that has a <asp:buttonField> of
I have a GridView that I would like to bind to an ObjectDataSource. I
I have a GridView that allows editing the values in every column, in every
I have a gridview control that can get loaded with multiple pages worth of
Greetings, I have gridview with SelectedIndexChanged event. when I click on a record in
I have a gridview that could end up displaying about 5K records. I currently
I have a Gridview that shows a list of Contacts via a sqlDataSource. Included

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.