P R
P R

Reputation: 334

Access GridView inside another GridView from code-behind

I create in ascx page 3 GridViews, like:

<dxwgv:ASPxGridView ID="grid1" ..... >
    <dxwgv:ASPxGridView ID="grid2" .... >
      <dxwgv:ASPXGridView ID="grid3" ....>
      </dxwgv>
    </dxwgv>
 </dxwgv>

But in code-behind I see only first grid (grid1) ID and can control only it. How to use others?

Upvotes: 0

Views: 2860

Answers (3)

Niranjan Singh
Niranjan Singh

Reputation: 18290

A better solution is to assign unique IDs (and ClientInstanceNames), as well as scripts, to controls at runtime. This approach is described in the following Knowledge Base article: The general technique of using the Init/Load event handler.

and then another approach is that handle the ASPxGridView.DataBound event of the detail grid and get a reference to the grid via the sender parameter. Here you can call the ASPxGridView.FindDetailRowTemplateControl method of the master grid if you are using Master Details.

If you are using GridView's DataRowTemplate then use the ASPxGridView.FindRowTemplateControl Method, you just need to get the visibleIndex of the row and you are able to access the grid with it's name.

If you are using Coloumn template then use ASPxGridView.FindRowCellTemplateControl Method

 protected void ASPxGridView1_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e) {
        if(e.DataColumn.FieldName == "title") {
            ASPxTextBox textBox = ASPxGridView1.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "ASPxTextBox1") as ASPxTextBox;
            textBox.Text = Convert.ToString(e.CellValue);
        }
    }

Reference these:
ASPxGridView - How to access controls inside DetailRow on the client side

Upvotes: 0

abatishchev
abatishchev

Reputation: 100268

I think

GridView grid2 = (GridView)grid1.FindControl("grid2")
GridView grid3 = (GridView)grid2.FindControl("grid3")

should work.

Upvotes: 1

Alex
Alex

Reputation: 6149

You will not see the other gridviews since they are hidden in the first grid view, to access the other gridviews you should do the following:

  1. create by code two grid view controls lets say name them: GVsubone and GVsubtwo
  2. in the RowDataBound of the first gridview (the one visible for you) make your GVsubone handles the events of your grid2 like this grid2.RowDataBound += new EventHandler(GVsubone.RowDataBound);
  3. and then in the GVsubone RowDataBound you have to do the same logic to handle the events for grid 2

P.S. you can handle any event RowDataBound was an example.

Upvotes: 0

Related Questions