Access GridView inside another GridView from code-behind - c#

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?

I think
GridView grid2 = (GridView)grid1.FindControl("grid2")
GridView grid3 = (GridView)grid2.FindControl("grid3")
should work.

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:
create by code two grid view controls lets say name them: GVsubone
and GVsubtwo
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);
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.

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

Related

Remove Edit, Update, and Cancel links from a RadGrid

I am trying to remove the links that are displayed in a Telerik RadGrid by default. Here is what the grid looks like before I try to remove the edit link:
I have found this snippet of code, it is used to remove the edit link:
if (!IsPostBack)
{
foreach (GridItem item in RGV_POI.MasterTableView.Items)
{
if (item is GridEditableItem)
{
GridEditableItem editableItem = item as GridDataItem;
editableItem.Edit = true;
}
}
RGV_POI.Rebind();
}
This is how the grid looks after the code:
The edit link still shows up on the first item. Is there a way to remove the edit, update, and cancel link on each item in the RadGrid? I want to be able to remove/disable the links, using a button click event. Then be able to add/enable the links back, using a button click event.
I'm not aware of Telerik RadGrid Control, but for sure the control should inherit asp:GridView. You can make links not visible in RowDataBound event. Here how you can do it.
Add OnItemDataBound="Grid_ItemDataBound" on the grid view.
In the code behind:
protected void Grid_ItemDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Item.DataItem == null)
return;
//cell of all the link button edit/update etc.
TableCell cell = e.Item.Cells[//index of the column];
foreach(Control c in cell.Controls)
{
c.Visible = false;
}
}
You should check the ID of the cancel, edit, update buttons somehow. Probably you should give more information about the controls in the aspx.
EDIT:
Use OnItemDataBound event it existing in their documentation: http://www.telerik.com/help/aspnet-ajax/events_t_telerik_web_ui_radgrid.html
The edit link button in a RadGrid is actually a column itself, specifically a GridEditCommandColumn. In order to show/hide this in the event of a button click, you would have to essentially rebuild all the columns programatically in the click event handler, including or excluding the GridEditCommandColumn as needed. You cannot add or remove a single column programatically when the rest of the grid is created declaratively. It would be useful if we could see more of how the grid is declared and built in your application.
Creating a RadGrid Programatically
It may be possible to change the GridEditCommandColumn.Display property, however. If you can get a handle on the column itself, instead of the individual cells, you may be able to adjust this as needed in your button click events.
You should remove the GridEditCommandColumn if you do not want your items editable. Another option is to change its visibility on the server through its Visible/Display property. You can use the grid's GetColumnSafe(columnName) method to get the neded reference: http://www.telerik.com/help/aspnet-ajax/grid-using-getitems-getcolumn-methods.html
To get rid of the update/cancel buttons, you can use a custom template, although I do not see why you would need to do that if your grid is not editable: http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx

Gridview button not firing its method

I populate a gridview called grdOrder by using a method which uses a series of other classes and stored procedures (it's so ugly that even I don't understand because it's given by our professor and he's confused us on whole new level)
I then want to delete an item from the database based on value (OrderId) which is in the second rows in the GridView once it is populated.
In the row editor of Gridview I created a button and when I double click the gridview in design view it creates the follow
protected void grdOrder_SelectedIndexChanged(object sender, EventArgs e)
So that is what I think should be firing when I hit the button remove on the Gridview. So I put a break underneath that method and it does not fire when I do that on the form in the browser. So it's not working basically.
If you want to see more of what's going on here is the full codes
http://pastebin.com/iSvyJCPQ - Aspx page
http://pastebin.com/LVRvaCnu - Code Behind (C#)
use the Gridview.RowCommand event
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

Command behind a GridView ButtonField

I got a question about the command behind a ButtonField (image type) in a GridView.
Got a GridView called gvIngevuld in wich I show rows of data and 1 ButtonField row.
Now I have to put code behind these buttons (Each of them the same) to put some things in PDF format.
The problem is I don't know how to put code behind these buttons ?
The code I have :
<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/pdf.png" CommandName="pdf_click" />
As you can see I used the CommandName="pdf_click" property but when I click one of the buttons there's only a postback but nothing happens.
Anyone who can help me out here ?
in case needed, code behind :
protected void pdf_click(object sender, EventArgs e)
{
lblWelkom.Text = "Succes!";
}
Thanks.
You should use RowCommand event of gridview. Set the commandArgument to your item's unique key. (By default, it passes the row's index)
void gvIngevuld_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="pdf_click")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = gvIngevuld.Rows[index];
//gbIngevuld is your GridView's name
// Now you have access to the gridviewrow.
}
}
Also, If you have set the DataKeyNames of the gridview, you can access it as follows:
int index = Convert.ToInt32(e.CommandArgument);
int ServerID = Convert.ToInt32(GridView1.DataKeys[index].Value);
Just an addition, I wrote and tried exactly the same however the button still not working...
...it took some time till I realise that I forgot a thing, which was
OnRowCommand="<YourGridViewName>_RowCommand"
inside your GridView element in the **.cs* file
The CommandName property does not define the method to call. For that you need to create a method that is bound to the command event of the control.
I don't know too much about about GridViews i'm afraid but I would think the easiest way to do this would be to select it in design view in Visual Studio, go to properties, click on the events button (looks like a lightning flash) and double click on the RowCommand event propety. This should automatically create a method that is bound to a command button event.
You can then use the GridViewCommandEventArgs parameter to access the CommandName, CommandArgument and CommandSource properties to work out which buttom caused the event to fire.

PostbackUrl of dynamic GridViews not what I think they should be

I have a couple GridViews that are dynamically created and placed into a PlaceHolder. When I mouse over the Select button, it shows __doPostBack('ctl00$bodyPlaceHolder$ctl0X','Select$Y'), where X = what I think is the GridView/Control index for the page and Y = row number of that GridView.
Since it is dynamically creating the GridViews, it makes sense that names them ctl0X, but on the PostBack how do I use this information?
I wouldn't even have this problem if adding the SelectedIndexChanged EventHandler worked, but it never gets called.
I found one other question like this, but the answer involved adding a GridView within my GridViews, which would also have to be dynamic, which brings me back to the original problem.
Edit
Okay, so I set gridViewDynamic.ID = "blahblah" + r.LastName, thus giving each GridView a unique name, so on mouseover in the page I get __doPostBack('ctl00$bodyPlaceHolder$blahblahSmith',Select$Y, I still can't access the items on PostBack because they no longer exist. So, I added the same GridView creation code to an if(IsPostBack), then called GridView gView = (GridView)this.Page.FindControl(blahblahSmith). Great, gView isn't null. But all the data in the rows are. Calling gView.Rows[0] returns null.
Use Page.FindControl("TheNameYouGaveTheDynamicGridView")
GridView grid = Page.FindControl("TheNameYouGaveTheDynamicGridView") as GridView;
If you are using MasterPages, you need to take a different approach to find the control on the page, but it is the same premise.

How to add a reference to a method in a custom control to a child control created in that same custom control

I have a custom control that is based off the gridview control located at: here
The control is basically a gridview that automatically creates a column of checkboxes that can be used to "check" individual rows in the gridview.
During the gridview's "CreateColumns" event, the "checkboxcolumn" is created dynamically. The checkboxcolumn also contains another checkbox in the header that is used to "select/deselect all" checkboxes in the column.
Since the gridview does not automatically remember the state of the checkboxes in the checkboxcolumn on postback, I added a method to the control called "SaveCheckBoxState" which stores the indexes of the checked rows in Viewstate, and then I modified the "OnRowDataBound" event to check the Viewstate and reset the checkboxes based on the Viewstate.
I then added a call to "SaveCheckBoxState" in the gridview's OnSorting and OnPageIndexChanging events. This works great so long as I'm sorting or changing pages.
However, I need it to update the viewstate everytime someone clicks or unclicks one of the checkboxes. At this time, the checkboxes are rendered with an onclick event that calls some javascript to highlight the row, or in the case of the checkbox in the header, to select/deselect all checkboxes.
I need to call the "SaveCheckBoxState" method from the javascript used by the customcontrol, or I need to find a way to modify viewstate from javascript and perform the same action as "SaveCheckBoxState".
I've tried adding the "SaveCheckBoxState" to the onclick event declaration in the checkboxes, but when run, it simply tells me that the method is undefined. It doesn't exist in the parent page, and I don't think I should have to make an event for the parent page to pass the click to. It seems to me this should be all self contained within the custom control.
Does anyone know how I can acheive this?
Here is the code for the gridview OnPreRender event where the onclick event of the checkbox is set:
protected override void OnPreRender(EventArgs e)
{
// Do as usual
base.OnPreRender(e);
// Adjust each data row
foreach (GridViewRow r in Rows)
{
// Get the appropriate style object for the row
TableItemStyle style = GetRowStyleFromState(r.RowState);
// Retrieve the reference to the checkbox
CheckBox cb = (CheckBox)r.FindControl(InputCheckBoxField.CheckBoxID);
// Build the ID of the checkbox in the header
string headerCheckBoxID = String.Format(CheckBoxColumHeaderID, ClientID);
// Add script code to enable selection
cb.Attributes["onclick"] = String.Format("ApplyStyle(this, '{0}', '{1}', '{2}')",
SelectedRowStyle.CssClass,
style.CssClass,
headerCheckBoxID);
// Update the style of the checkbox if checked
if (cb.Checked)
{
r.BackColor = SelectedRowStyle.BackColor;
r.ForeColor = SelectedRowStyle.ForeColor;
r.Font.Bold = SelectedRowStyle.Font.Bold;
}
else
{
r.BackColor = style.BackColor;
r.ForeColor = style.ForeColor;
r.Font.Bold = style.Font.Bold;
}
}
}
You should have your custom GridView control subscribe to the CheckChanged event of the checkboxes. It should call its SaveCheckBoxState method on its own. That method should be private.
Parent and child controls should only communicate via properties and events, never through methods and fields.

Categories