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();
}
Related
I have a grid view and in this grid some rows have a field called "Department". What I want is a code that reads that fields value and then if it equals a string "Industrial" the row should be hidden and not shown.
I tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (e.Row.Cells[4].Text == "industrial")
e.Row.Visible = false;
}
But it keeps saying (e) is not defined, and there is no such thing as (.Row).
It's GridViewRowEventArgs that have access to e.Row but it's used in RowCreated and RowDataBound events. You can use SelectedRow property of GridView instead.
var gridView = (GridView)sender;
if (gridView.SelectedRow.Cells[4].Text == "industrial")
gridView.SelectedRow.Visible = false;
Updated:
To hide the rows when page is loaded, place the for loop inside the Page_Load event.
protected void Page_Load(object sender, EventArgs e)
{
// ...
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[4].Text == "industrial")
GridView1.Rows[i].Visible = false;
}
}
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;
}
I am using GridView with AutoGenerateEditButton="True" which gives me edit functionality. My problem is, if I want to get cell value in RowEditing event than I can get it by using grdEmpDetail.Rows[e.NewEditIndex].Cells[2].Text but if I want to get cell value in RowCancelingEdit event than grdEmpDetail.Rows[e.RowIndex].Cells[2].Text is always empty.
How can I get the Cell value in RowCancelingEdit event?
CODE:
protected void grdEmpDetail_RowEditing(object sender, GridViewEditEventArgs e)
{
//Reset the edit index.
this.grdEmpDetail.EditIndex = e.NewEditIndex;
//Bind data to the GridView control.
WorkSampleBusiness.BusinessObject objBL = new WorkSampleBusiness.BusinessObject();
if (!String.IsNullOrEmpty(grdEmpDetail.Rows[e.NewEditIndex].Cells[2].Text)) //This gives me current cell value
{
lastName = grdEmpDetail.Rows[e.NewEditIndex].Cells[2].Text;
DataBindGrid(objBL.EmployeeDetails(lastName));
}
else
DataBindGrid(objBL.EmployeeDetails(lastName));
}
protected void grdEmpDetail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//Reset the edit index.
this.grdEmpDetail.EditIndex = -1;
//Bind data to the GridView control.
WorkSampleBusiness.BusinessObject objBL = new WorkSampleBusiness.BusinessObject();
if (!String.IsNullOrEmpty(grdEmpDetail.Rows[e.RowIndex].Cells[2].Text)) // This is always empty
{
lastName = grdEmpDetail.Rows[e.RowIndex].Cells[2].Text;
DataBindGrid(objBL.EmployeeDetails(lastName));
}
else
DataBindGrid(objBL.EmployeeDetails(lastName));
}
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?
'>
'>