I have a UserControl which contain GridView.I set AutoGenerateSelectButton is true for this GridView.
But it didn't work when I press Select inside UserControl.
Do you have anyidea?
You should move your event handling to the page that owns the UserControl and not in the UserControl
Inside the Page_Load of your page, add this
myUserControl.FindControl("GridView1"));
dvGrid.SelectedIndexChanged += new EventHandler(GridView1_SelectedIndexChanged);
Add the handler to the page
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
//access the GridView
GridView grid = (GridView) sender;
//access the selected row
GridViewRow selectedRow = grid.SelectedRow;
//access the selected Primary key - make sure you set the DataKeyNames property of the GridView to the Record Id - in your Markup
string currentRowPrimaryKey = grid.SelectedValue;
//OR
string currentRowPrimaryKey = grid.SelectedDataKey.Value;
}
Now you have several values to play with. You can put a break point and examine the properties of the sender to have more options. Good Luck
Related
i have a gridview with devexpress my gridview contain a id ID="GriviewLV1" and i have a button in the row of the gridview to edit the record , in the event click of the button im trying give click in the button to edit but cannot get the property rows or something like that. im trying something like this but cannot do rows property because dont exist the rows property in grid view devexpress
button_Edit_click(object sender, EventArgs e)
{
foreach (GridViewRow row in GriviewLV1.Rows)
{
}
}
And for what exactly do you need row collection?
As far as I know, you can only manipulate with records (DataSource property object). Also it is possible to get selected row. Some my code examples with explanation:
private List<ImageSetMember> imageSets;
...
//assign collection as grid's DataSource.
//From now on any actions on imageSets object will be automatically reproduced
//as grid's row changes.
imageSetGridControl.DataSource = imageSets;
...
private void replaceButtonEdit_Click(object sender, System.EventArgs e)
{
//get focused row record's index at DataSource collection
int index = imageSetGridView.GetDataSourceRowIndex(imageSetGridView.FocusedRowHandle);
var selectedImage = imageSet[index].Image; //accessing to row's record
}
i inserted a drop down list into my grid view from the code behind like it is done here
Convert gridview field into Dropdownlist
i need to make command in the row that contains this dropdownlist when its selected index is changed.
please can some one hepl me
thanks
If you need to get the GridViewRow of a DropDownList which was generated dynamically use following approach.
Add the same SelectedIndexChanged-event handler to the dropdowns. Handle it, cast sender to DropDownList, cast it's NamingContainer to GridViewRow. Now you are all set.
protected void DropDown_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList) sender;
GridViewRow row = (GridViewRow) ddl.NamingContainer;
// now you get the reference to the other controls via row.FindControl
}
I have a column with a button in my radgrid, I want to be able to click the button and get the values of the row in which the button is located on. This must be done without selecting the row, I remember seeing a part of code my friend wrote in which he used ".Parent" or something similar to get the row from of the button column clicked.
When you click any button in the GridButtonColumn of a RadGrid, you can access the button's row and the row's values by doing the following:
In the RadGrid's definition, add OnItemCommand="RadGrid1_ItemCommand".
In the GridButtonColumn's definition, set its command name with something like CommandName="Test".
Now add the following to your code-behind to access whatever column you want. In my sample code below I get the value in the column "Whatever":
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "Test")
{
GridDataItem item = (GridDataItem)e.Item;
string columnWhateverValue = item["Whatever"].Text;
}
}
So, I have a GridView on my MasterPage with an onclick Event which returns the ID (there are device components in this grid and I need their ID from the database) from that row.
The Onclick event which is bound in the RowDataBound event
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.selectionGridView, "Select$" + e.Row.RowIndex);
When I click a row the SelectedIndexChanged Event gets fired
protected void selectionGridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridView selectedGridView = sender as GridView;
//Saves the Database ID of the selected data
Session["selectedDevID"] = selectedGridView.SelectedRow.Cells[0].Text;
_Auswahl.FillTree();
}
And this piece of code calls a method from the page I want to show a Treeview with more stuff etc.
_Auswahl.FillTree();
Everthing is fine, all methods are called correctly, but on the _Auswahl Page, I can't add things to the TreeView (or to the Page at all). All my controls return a NullReferenceExeption, and if I initialize them, then this stuff won't show up
I tried everything, I guess I miss some little thing or forgot something, but I can't get this thing right ._.
Edit: I don't want to Redirect to that Page again, I want to "refresh" the treeview (which is located in an UpdatePanel)
Edit2:
This is my ___FillTree()_ Methode (Excluded the whole Linq stuff etc etc und left just some basic stuff)
public void FillTree()
{
//Add child node value
TreeNode child = new TreeNode("Manager");
//Add parent value
TreeNode parent = new TreeNode("Development");
//Add child to parent
parent .ChildNodes.Add(child);
//Add parent to tree
TreeView1 = new TreeView();
TreeView1.Nodes.Add(parent);
TreeView1.DataBind();
TreeView1.ExpandAll();
}
I have tried this but its not working,giving null value:please tell me what have to change in this code.
protected void Button1_Click(object sender, EventArgs e)
{
GridViewRow row;
row = dgData.Rows[0];
DropDownList ddl= (DropDownList)(row.Cells[1].FindControl("ddlCol1"));
}
use
DropDownList ddl= (DropDownList)(row.FindControl("ddlCol1"));
and try and let me know
try this code on click event
foreach (GridViewRow row in GridView1.Rows)
{
//Finding Dropdown control
DropDownList ddl1 = row.FindControl("ddlTest") as DropDownList;
if (ddl1 != null)
{
// your code here
}
}
#creby i saw your code.
you are adding your dropdown in grid view during row data bound event ok.. but now when you click on the button, all the drop down will be vanished so that's why you will not able to get drop down.
use item template of grid view and then bind dropdown in row data bound event.