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));
}
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 am trying to add the column names of a gridview to a dropdown list.
The problem is when I change the datasource of the gridview, it returns empty.
I am not sure whether the event is working properly, as it does not work with/without it.
I wanted to use the DataBindComplete event, but I could not see it so I tried DataBound instead.
private void BindTable()
{
if (ddTableSearch.SelectedIndex == 0)
{
tblCustomerTableAdapter customerAdapter = new tblCustomerTableAdapter();
GridView2.DataSource = customerAdapter.GetData();
GridView2.DataBind();
}
else if (ddTableSearch.SelectedIndex == 1)
{
tblInvoiceTableAdapter invoiceAdapter = new tblInvoiceTableAdapter();
GridView2.DataSource = invoiceAdapter.GetData();
GridView2.DataBind();
}
else if (ddTableSearch.SelectedIndex == 2)
{
tblEstimateTableAdapter estimateAdapter = new tblEstimateTableAdapter();
GridView2.DataSource = estimateAdapter.GetData();
GridView2.DataBind();
}
}
protected void GridView2_DataBound(object sender, EventArgs e)
{
// Populate dropdown with column names
ddColumnSearch.Items.Clear();
for (int i = 0; i < GridView2.Columns.Count; i++)
{
ddColumnSearch.Items.Add(new ListItem(GridView2.Columns[i].ToString()));
}
}
What am I doing wrong?
The databound event runs for each record in the gridview. So one issue is that each time you are clearing out the items you added before. Another issue is that you need to get the data from the EventArgs instead of from the gridview.columns since those are not there until after all the data is bound. I think all you need to do is get the data from the header row:
protected void GridView2_DataBound(object sender, EventArgs e)
{
// Populate dropdown with column names
if(e.Row.RowType != DataControlRowType.Header) return; //only continue if this is hdr row
ddColumnSearch.Items.Clear();
foreach (TableCell cell in e.Row.Cells)
{
ddColumnSearch.Items.Add(new ListItem(cell.Text));
}
}
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 am populating a datatable, then binding it to a gridview. Now I'm reading the rows in the grid and coloring the row if value = [x].
The thing when I try to display on the page the row that is colored im getting it duplicated.
Lets say i have colored 1 row but the response.write will be like 100 times the same result.
Below is my code, hope someone can help :
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string alert = Request.QueryString["check"];
// loop over all the Rows in the Datagridview
foreach (GridViewRow row in gv1.Rows)
{
// if condition is met color the row text
if (gv1.Rows[0].Cells[0].Text.ToString() == alert)
{
Session ["fn"] = gv1.Rows[0].Cells[2].Text;
gv1.Rows[0].ForeColor = Color.Red;
}
Response.Write(Session["fn"]);
}
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string alert = Request.QueryString["check"];
if (e.Row.Cells[0].Text.ToString() == alert)
{
Session ["fn"] = e.Rows.Cells[2].Text;
e.Rows.ForeColor = Color.Red;
Response.Write(Session["fn"]);
}
}
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();
}