0
votes

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

1

1 Answers

0
votes

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