I have a dynamically generated gridview with custom datasource.
The gridview allows “editing, inserting, and deleting”
Everything (all features) are working fine..
I then added a filtering feature to the gridview..
Let’s say previously my datasource is based on this select statement:
SELECT * FROM <Table>
Now, after filter:
SELECT * FROM <Table> WHERE <Filter Condition>
After filtering.. I am not able to do the editing (updating) gridview properly.. As during the editing mode..
public void gvGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
gvGridView.EditIndex = e.NewEditIndex;
((TemplateField)gvGridView.Columns[1]).EditItemTemplate = null;
//disable editing on primary key
if (txtSearch.Text != string.Empty || CountRow() > 0)
{
FilterQuery(); //refilter the gridview
}
else
{
gvGridView.DataBind();
//if gridview is not meant to be filtered, just leave it as it is
}
Session["SelecetdRowIndex"] = e.NewEditIndex;
}
Due to this line:
gvGridView.EditIndex = e.NewEditIndex;
The editindex will not be initialized correctly. For example:
-
Before filter it was index no 8
-
After filter it is index no 1
If i edit this gridview after filter what it will do is it will take 1 as the edit index (supposedly 8)… hence instead of updating data on index 8.. it will override data with index number 1…
I hope my question is clear enough.. looking forward to hear some answers :)..
Edit:
This what happens during gridview updating:
GridViewRow row = gvGridView.Rows[e.RowIndex];
//decide which row being edited..
for (int i = 0; i < Table.Columns.Count; i++)
{
string field_value = ((TextBox)row.FindControl(Table.Columns[i].ColumnName)).Text;
ParameterArray.Add(field_value);
//storing all value in that row into an array (including PK)
}
The code above works fine (store all value into array) except for the primary key.. (always store e.rowindex value)..
In row updating event in cs page:
In aspx page:
so you will get the ID.You can update using the Id now.Not with the Index.