I have an autogenerated DataGridand I want to retrieve the ID value. There is a HyperLink button on each row and I want to send the value of that row's ID to a new page via query string. The order of ProductGrid is as follows
(HyperLinkButton,ID,Name,Details).
This is how I am binding the Grid:
public void webService_GetProductsCompleted(object sender, ServiceReference1.GetProductsCompletedEventArgs e)
{
ProductGrid.ItemsSource = e.Result;
ProductGrid.Visibility = Visibility.Visible;
}
And this is the HyperLink button click event where I tried retreiving the cell value but got null(commented)
private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
//string var = ProductGrid.Columns[1].GetCellContent(ProductGrid.SelectedItem).ToString();
this.NavigationService.Navigate(new Uri("/Home?", UriKind.Relative));
}
Can somebody help me in finding out how can I send my cell value in a query string?
You can use RowCommand event or try this way.
private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
{
LinkButton lnkbtn= sender as LinkButton;
//getting particular row linkbutton
GridViewRow grow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
string prodid = ProductGrid.DataKeys[gvrow.RowIndex].Value.ToString();
string prodname = grow.Cells[0].Text;
}
Related
hello i want to redirect to another page if label in repeater value equals C# and the button is clicked.
here is my code
protected void Button1_Click(object sender, EventArgs e)
{
RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem;
String name;
name = (item.FindControl("lblname") as Label).Text;
if (!IsPostBack)
{
if (name=="Csharp") { Response.Redirect("Csharp.aspx"); }
}
}
I have a Gridview and a RepositoryItemGridLookUpEdit in that GridView
I want to show a CustomDisplayText in the RepositoryItemGridLookUpEdit
private void rgluePerson_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
{
var person = rgluePerson.GetRowByKeyValue(e.Value) as Person;
var name = person.Name;
var surname = person.Surname;
e.DisplayText = name + ", " + surname;
}
}
The problem is that the person name depends of another cell in the same row (in the main Gridview), and i don't know how to get the current's Gridview row being processed (current row doesn't work since i need the row being processed in the moment)...... i can't use a gridView event cuz it will change the cell value, but i want to change the Text value.
Does anyone knows how to do that?
You cannot get the row being processed by the CustomDisplayText event because there are no such fields or properties that contains current row. You can only use this event for focused row. For this your must check if sender is type of GridLookUpEdit:
private void rgluePerson_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
{
if (!(sender is GridLookUpEdit))
return;
var anotherCellValue = gridView1.GetFocusedRowCellValue("AnotherCellFieldName");
//Your code here
e.DisplayText = yourDisplayText;
}
For not focused rows you can use only ColumnView.CustomColumnDisplayText event:
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
if (e.Column.ColumnEdit != rgluePerson)
return;
var anotherCellValue = gridView1.GetListSourceRowCellValue(e.ListSourceRowIndex, "AnotherCellFieldName");
//Your code here
e.DisplayText = yourDisplayText;
}
I have created a GridView as below:
protected void Page_Load(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.ID = pId.ToString();
gv.AutoGenerateEditButton = true;
gv.DataKeyNames = ids;
gv.RowEditing += gv_RowEditing;
gv.RowUpdating += gv_RowUpdating;
bindGv(pId, gv);
}
I have also written the following methods:-
RowUpdating:
void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView gv = sender as GridView;
GridViewRow row = (GridViewRow)gv.Rows[e.RowIndex];
ProductCategory pc = context.ProductCategories.First(s => s.Name ==gv.ID );
TextBox txtName = row.FindControl("txtName") as TextBox;
pc.Name = txtName.Text;
}
RowEditing:
void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gv = sender as GridView;
gv.EditIndex = e.NewEditIndex;
bindGv(Convert.ToInt32(gv.ID), gv);
}
But when I ran the codes in debugging mode, clicking on the update button invokes gv_RowEditing method instead of gv_RowUpdating. What is the problem?
The Sequence of firing the events of gridview when you click on edit button to update any record it calls rowediting event and after that when you click on update button it calls rowupdating event.
rowediting event always invokes first then rowupdating.
I have a requirement to replace a label in gridview when I click on the Edit button added manually in grid view.
Please see below the code:
protected void btnchange_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
int getindex = gvr.RowIndex;
gvorderdetail.Rows[getindex].Cells[3].Style.Value = "red";
gvorderdetail.Rows[getindex].Cells[0].Attributes["style"] = "background-color:Red";
gvorderdetail.Rows[getindex].Cells[1].Attributes["style"] = "background-color:Red";
gvorderdetail.Rows[getindex].Cells[2].Attributes["style"] = "background-color:Red";
TextBox tbx = (TextBox)gvorderdetail.Rows[getindex].FindControl("txtitemqty");
}
protected void gvorderdetail_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "change")
{
TextBox tbx = (TextBox)gvorderdetail.Rows[0].FindControl("txtitemqty");
}
}
Can anyone help?
'>
'>
How do I use the AutoGenerateEditButton to update my gridview Table (has a dataset bound to it -Dataset Retrieved from an sql database)
--Removed the Broken code in the question to put Fixed code in the Answer--
To get the values of the updated row add this to your "RowUpdating" event handler
protected void grdViewDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)grdViewDetails.Rows[e.RowIndex];
foreach (Control item in row.Controls)
{
if (item.Controls[0] is TextBox)
{
TextBox textbox = (TextBox)item.Controls[0];
string x = textbox.Text; //theres your value you can do stuff with
}
if (item.Controls[0] is Label)
{
Label mylabel = (Label)item;
//do stuff - just do the same as the textbox
}
}
}
and in "RowEditing" event handler
protected void grdViewDetails_RowEditing1(object sender, GridViewEditEventArgs e)
{
grdViewDetails.EditIndex = e.NewEditIndex;
//e.newedit index:- will be provide index of row for which edit button is selected
grdViewDetails.DataSource = yourdatasource //mine was a datset
grdViewDetails.DataBind();
}