0

I want to perform operation that On a button click event , Grid Currentrow entire data is passed to an object array

I have tried to search through following links :

DataGridView selecting a specific row and retrieving its values

Getting data from selected datagridview row and which event?

But they are talking about particular cell value

i tried to perform with code

DataRowView currentDataRowView = (DataRowView)grdGLSearch.CurrentRow.DataBoundItem;
DataRow row1 = currentDataRowView.Row;

But currentDataRowView is retrieving null

one of My Senior succesfully created a generic property GetSelectedRow()

it works like this :

  var object =grdGLSearch.GetSelectedRow<T>();

and it has definition

public T GetSelectedRow<T>()
        {
            if (this.CurrentRowIndex == -1)
            {
                return default(T);
            }
            return (base.DataSource as BindingList<T>)[this.CurrentRowIndex];
        }

But it is binded to only one Main grid , i also want to use this property to another Grids

I dont want data of a particular column , i want entire row data .. and dont want any iteration to be perform ... Is there any single liner operation for this ? Please suggest if I am missing any links

Community
  • 1
  • 1
neeraj
  • 345
  • 2
  • 6
  • 19

1 Answers1

0

Since you are only showing 2 lines of code, I am really not sure exactly what is going on. First off, I get you want the entire row when you click some button. However, you never state how the current row is being selected.

In order to get the full selected row, you must have a selected cell. The MSDN documentation says this on the page for DataGridView.SelectedRow. So I am assuming that the user will click a cell, and you want the entire row. I would create a variable that will hold your selected row. Then when the user clicks the cell, automatically select the row and save it. Then when the button is clicked, just retrieve the the already saved row.

private DataGridViewRow selectedRow { get; set; }

Then have the event for when the user clicks a cell

private void grdGLSearch_CellClick(object sender, DataGridViewCellEventArgs e)
{
    selectedRow = grdGLSearch.Rows[e.RowIndex];
}

Finally, the button click event

private void SubmitBtn_ItemClick(object sender, ItemClickEventArgs e)
{
    // to target the specific data
    var cellVal1 = selectedRow.Cells["SpecificCell1"].Value;
    var cellVal2 = selectedRow.Cells["SpecificCell2"].Value;
}
Tom
  • 1,047
  • 3
  • 24
  • 44
  • yes but selectedRow will give all metadata information about that row , i dont want all metadata information ,I just want values collection inside the columns – neeraj Feb 15 '13 at 07:00
  • Then in your button click method, pull that information out of the selectedRow. – Tom Feb 15 '13 at 17:50
  • my question is same , i want code by which i can pull that information out of the selectedRow – neeraj Feb 18 '13 at 13:20
  • I edited my answer at the SubmitBtn_ItemClick event. You can pull out the cell info from the selectedRow.Cells array. If you don't want to reference each cell individually, you can just iterate over the selectedRow.Cells collection itself to pull out the values. If you already have a class that holds properties for each value, you can just write a mapping method. One simple way would be to grab a dictionary of the index and string value of the cell. Then map your class by providing the dictionary and mapping the appropriate index with the value and casting accordingly. – Tom Feb 19 '13 at 15:36