locked
Get business object for a GridView row RRS feed

  • Question

  • User197122282 posted

    I am using business objects or DTOs in my application. I have the following class:

    public class Customer
    {
        private int customerID;
        prviate string name;
    
        public int CustomerID
        {
             get { return customerID; }
             set { customerID = value; }
        }
    
        public string Name
        {
             get { return name; }
             set { name = value; }
        }
    }

    I create a Collection of the custom object and assign it to the gridview:

    ICollection<Customer> customerList;
    customerList = new List<Customer>();
    
    
    Customer c1 = new Customer ();
    c1.CustomerID = 1;
    c1.Name       = "John";
    customerList.Add (c1);
    
    
    Customer c2 = new Customer ();
    c2.CustomerID = 2;
    c2.Name       = "Kevin";
    customerList.Add (c2);
    
    gvCustomer.DataSource = customerList;
    gvCustomer.DataBind ();

    Now I want to get back the whole business object for any row. For example, if I want the business object from the first row, I want to do something following:

    Customer c = (Customer) gvCustomer.rows [0].DataItem;

    However I receive the error "Object reference not set to an instance of an object" . I know I can get the object during the RowDataBound event, however; in my case, I need to get the object value on a button click event.




    Friday, September 27, 2013 4:23 PM

All replies

  • User-1716253493 posted
    You can bind Button CommandArgument then use RowCommand event or click event like : LinkButton btn=(LinkButton) sender; string id=btn . CommandArgument ;
    Friday, September 27, 2013 4:46 PM
  • User-1716253493 posted
    You can bind Button CommandArgument then use RowCommand event or click event like : LinkButton btn=(LinkButton) sender; string id=btn . CommandArgument ;
    Friday, September 27, 2013 4:46 PM
  • User197122282 posted

    oned, I think you didn't get my question. I already mentioned that I need to access all/specific row(s) on a specific event. For example, on a button Click event, I want to access each row, get the business object and update it in the database. It's like doing a bulk update for all rows on a button click event.

    Friday, September 27, 2013 4:52 PM