2

If I wanted to add a new item to a <List> with the Data key name from a gridview how would I go about doing it?

Example.

List<myClass> myList  = new List<myClass>();

myList.add(new myClass(*//The value of the data key name that has been clicked*));

A user might click on another item so it would be repeated etc etc.

1
  • What event are you wanting to do this in? Commented Nov 30, 2010 at 18:11

2 Answers 2

1

@Brian: Check it out:

    protected void dataGridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {   
        if(myList == null)        
          myList = new List<myClass>();
        foreach (object keys in dataGridView1.DataKeys[e.NewSelectedIndex].Values)
        {
            myList.Add(keys);
        }            
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You have to do this in rowSelected, and get the data key in a form of:

protected void Gridview_SelectedIndexChanged(..)
{
   GridViewRow row = CustomersGridView.SelectedRow;
  var key = this.gvw.DataKeys[row.RowIndex].Value;
}

Forget the exact syntax, but this is essentially what you do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.