FindControl not finding the correct control in GridView - c#

I have an issue with a GridView and a HiddenField inside the GridView. I'm trying to pull some data based in the value of the HiddenField which is basically the row_id for each record but for some reason I keep getting the same data regardless of which one I select in the GridView. The code below is the one I'm using the find the HiddenField in the GridView.
Any help will be greatly appreciated it.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
foreach (GridViewRow item in GridView1.Rows)
{
if (item.RowType == DataControlRowType.DataRow)
{
rowid = (HiddenField)(item.Cells[0].FindControl("po_id_hf"));
}
}
GridView2.DataSource = View_SP.v_asn_detail_by_asn_number(Int32.Parse(rowid.Value));
GridView2.DataBind();
step2.Visible = false;
step3.Visible = true;
}

What about using:
GridView1.Rows[e.RowIndex]
to get the current updating row?
EDIT: Your code will always return the last GridViewRows, since it is iterating through the whole GridViewRows collection so at the end rowid will have the last row id in the gridview.

I was over thinking this, below is the solutions:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string row_id = (((HiddenField)(GridView1.Rows[e.RowIndex].FindControl("po_id_hf"))).Value);
GridView2.DataSource = View_SP.v_asn_detail_by_asn_number(Int32.Parse(row_id));
GridView2.DataBind();
step2.Visible = false;
step3.Visible = true;
}
Thank you guys.

Related

select row from a gridview that during the execution has a new datasource

On the first column is the id and want to select it to be redirected to a page that has more details that row.
He goes good if datasource is not changed but if it is modified or receive ID of the initial data, or gives me error for that initially did not have many rows.
My code is:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.DataBind();
GridViewRow row = GridView1.SelectedRow;
string strCell = row.Cells[1].Text;
string myPageUrl = "Meci.aspx?ID=" + strCell;
Response.Redirect(myPageUrl);
}
Please tell me how can I change it to work well!

How to add Checkbox inside the Gridview dropdownlist

I need to do add Checkbox inside the Gridview dropdownlist, the dropdown list was added in RowDataBound event and it has fetch the data from database.
protected void grdupload_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
List<string> coll = new List<string>();
if (e.Row.RowType == DataControlRowType.DataRow)
{
string fnCriteria = ((DataRowView)e.Row.DataItem)["fnCriteria"].ToString();
DropDownList ddlfnCriteria = (e.Row.FindControl("ddlfnCriteria") as DropDownList);
coll.Add(fnCriteria);
ddlfnCriteria.DataSource = coll.ToList();
ddlfnCriteria.Text = fnCriteria;
ddlfnCriteria.DataBind();
}
}
Please suggest me to get a solution. Thanks in advance
You can follow these links.
1) http://www.dotnetgallery.com/kb/resource55-Checkbox-list-in-Dropdown-using-Aspnet-Ajax-PopupControlExtender-control.aspx
2) http://www.codeproject.com/Articles/66572/A-Multiple-Selection-DropDownList-a-CheckBoxList-I
3) http://www.dotnetspeaks.com/DisplayArticle.aspx?ID=63

How to replace text in some cells of GridView after data binding?

I have a GridView that is assosiated with database. Here's a data binding:
protected void GridViewProgramms_SelectedIndexChanged(object sender, EventArgs e)
{
int rowIndex = ((GridView) sender).SelectedIndex;
var programid = ((GridView) sender).Rows[rowIndex].Cells[1].Text;
GridViewEx.RowEditing += GridViewEx_RowEditing;
SqlDataSource1.SelectParameters["ID"].DefaultValue = programid;
GridViewEx.DataBind();
ExcersicePanel.Visible = true;
PanelAp.Visible = false;
}
Everything works fine, but I need to change some cells values in GridView after that. I need to rewrite every Cell in the last row. How to do this without affecting the database?
You will need to write your code(To change the cell text) in RowDataBound event of the gridview after data bind
Refer following link for more explanation
Update GridView Column after databinding
protected void GridViewEx_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
label or textbox ddltype = (label or textbox)e.Row.FindControl("id");
ddltype.text="ur text";
}
}

No Duplicates in DataGridView

I'm working in C# displaying some data in a DataGridView and I'd like it to not allow you to add a duplicate key.. Right now, my DataGridview is pretty simple with just 2 columns. One is called "Key" the other is called "Value". What i want, is for when the user edits or adds a new entry to the DataGridView, it checks if there is already a duplicate and cancels the edit/creation of the new row. Here is my current code:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[0].Value.Equals(dataGridView1.Rows[e.RowIndex].Cells[0].Value))
{
dataGridView1.Rows.Remove(dataGridView1.Rows[e.RowIndex]);
break;
}
}
refresh();
}
It isn't working at all... Can someone tell me how I should be doing this?.. Thanks!
Edit: I'm also getting this error on the dataGridView1.Rows.Remove() call -
Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.
Edit:
The DataGridView looks like this
Key | Value
----------------
blah | something
somekey | somevalue
It is probably your foreach loop, which doesn't allow deleting rows.
Try something like this:
private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
for (int i = 0; i < DataGridView1.Rows.Count; i++) {
DataGridViewRow row = DataGridView1.Rows[i];
if (row.Cells[0].Value == DataGridView1.Rows[e.RowIndex].Cells[0].Value) {
DataGridView1.Rows.RemoveAt(e.RowIndex);
DataGridView1.FirstDisplayedScrollingRowIndex = i;
return;
}
}
}
I do not allow row editing, though, so I could be wrong.
EDIT: Just a quick edit. I'd set the FirstDisplayedScrollingRowIndex to e.RowIndex, but that row would have just been deleted!
I'm only guessing but I've tried your code and I've seen a logical error:
if you debug it you'll see that with the foreach loop you'll necessary hit the element you just edited, which is obviously equal to itself.
I suggest you to change the loop to a for or to check if the row index is the same.
try with this code for that,
protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
DataTable dt1 = ViewState["dt"] as DataTable;
if (e.CommandName == "Add")
{
DataRow dr;
dr = dt1.NewRow();
dr["DED_Id"] = Material_Id;
dr["DED_Name"] = Material;
dt1.Rows.Add(dr);
dt1.AcceptChanges();
ViewState["dt"] = dt1;
}
}

Skipping Items During Data Binding in gridview c#

how to skip specific item in row bind event C#
You could
Filter the data via the SQL query.
Filter the data via a table select if binding to a datatable/dataset
Filter data via a DataView
Use the RowDataBound event
Edit ~ Here is how to use the RowDataBound
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow) {
//A value to check for
string myValue = DataBinder.Eval(e.Row.DataItem, "myColumn").ToString();
if ((myValue == "a")) {
//Hide the row
row.Visible = false;
}
}
}
I am not sure how this will affect your paging....if you do have one.

Categories