Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

Currently I have the gridview and a txtbox for the user to enter search value.
Once the set of filtered data is retrieved when I attempt to click edit to update
the row, it revert back to the first row on the grid. How can I edit the rows that are filtered?

I attempted to look online but couldn't find an example that would help me.

Can you provide a link of a demonstration as to how to perform CRUD operations after filtering data from a gridview?
Posted
Comments
JoCodes 12-Oct-13 4:09am    
Can u post your gridview rowediting event code???
Emmanuel Constant MSSM 16-Oct-13 10:00am    
Grid:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="White"
BorderStyle="Solid" BorderWidth="3px" CellPadding="4" DataKeyNames="EventID"
CellSpacing="2" ForeColor="Black" Width="984px" EmptyDataText="No Events Found"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<rowstyle backcolor="White">
<columns> <asp:CommandField ButtonType="Button" CausesValidation="False"
DeleteText="" EditText="Add Track ID" InsertText="" NewText=""
SelectText="" ShowEditButton="True" UpdateText="Commit" />
<asp:CommandField ButtonType="Button"
DeleteText="" EditText="" InsertText="" NewText=""
SelectText="Export" UpdateText="" InsertVisible="False"
ShowCancelButton="False" ShowSelectButton="True" CancelText="" />
<asp:BoundField DataField="CME_Event_Track_ID" HeaderText="CME Track ID" SortExpression="CME_Event_Track_ID" />
<asp:BoundField DataField="Event_Type" HeaderText="Event Type" SortExpression="Event_Type" Visible="False" />
<asp:BoundField DataField="Event_Name" HeaderText="Event Name" SortExpression="Event_Name" />
<asp:BoundField DataField="Event_Desc" HeaderText="Event Desc" SortExpression="Event_Desc" />
<asp:BoundField DataField="EventDate" HeaderText="Event Date" SortExpression="EventDate" />
<asp:BoundField DataField="EventCount" HeaderText="Attendance Count" SortExpression="EventCount" />
<asp:BoundField DataField="MissingRFID" HeaderText="Number Of Missing RFID" SortExpression="MissingRFID" />
<footerstyle backcolor="#CCCCCC">
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<emptydatatemplate>
 

<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

</td>
</tr>
<tr>
<td>
 </td>
</tr>
</table>


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CME_RFID %>" ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM CME_RFIDReader.vAdminExportStat order by Event_Date DESC"
UpdateCommand="UPDATE CME_RFIDReader.CME_Events SET CME_Event_Track_ID = @CME_Event_Track_ID WHERE EventID = @EventID">


Code Behind:

int RowCount;
protected void SearchEvent_Click(object sender, EventArgs e)
{

SqlDataSource1.FilterExpression = "Event_Name LIKE '%" + txtSearchEvent.Text + "%' or Event_Desc LIKE '%" + txtSearchEvent.Text + "%'";

GridView1.DataBind();
RowCount = GridView1.Rows.Count;
txtSearchEvent.Text = "";

}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int eventid;
eventid = Convert.ToInt32(GridView1.SelectedDataKey.Value);

String strCon = ConfigurationManager.ConnectionStrings["CME_RFID"].ConnectionString;
DataTable dtExport = new DataTable();
String strSQL = "SELECT PersonID, Person_Last, Person_First, Person_Title, Life_Number, Event_Name, EventID, CME_Event_Track_ID FROM CME_RFIDReader.vAttendanceList WHERE EventID = " + eventid;

SqlConnection connect = new SqlConnection(strCon);
SqlCommand command = new SqlCommand(strSQL, connect);

SqlDataAdapter adExport = new SqlDataAdapter(command);

adExport.Fill(dtExport);
Emmanuel Constant MSSM 16-Oct-13 10:03am    
Code behind Continued:

exportSpreadsheet(dtExport, "ExportReport");

}

public static void exportSpreadsheet(DataTable dtTable, String strName)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();


foreach (DataColumn column in dtTable.Columns)
{
context.Response.Write(column.ColumnName + ",");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow drRow in dtTable.Rows)
{
for (int i = 0; i < dtTable.Columns.Count; i++)
{
context.Response.Write(drRow[i].ToString().Replace(",", string.Empty) + ",");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + strName + ".csv");
context.Response.End();
}
Emmanuel Constant MSSM 17-Oct-13 16:00pm    
Hi again JoCodes,

I tried using the solution you provided
but I have the same results.

Code Behind (Row_editing):

public void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
((TemplateField)GridView1.Columns[1]).EditItemTemplate = null;
GridView1.DataBind();
Session["SelecetedRowIndex"] = e.NewEditIndex;
}

Gridview:

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="White"
BorderStyle="Solid" BorderWidth="3px" CellPadding="4" DataKeyNames="EventID"
CellSpacing="2" ForeColor="Black" Width="984px" EmptyDataText="No Events Found"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowEditing ="GridView1_RowEditing">
<rowstyle backcolor="White">
<columns>
<asp:CommandField ButtonType="Button" CausesValidation="False"
DeleteText="" EditText="Add Track ID" InsertText="" NewText=""
SelectText="" ShowEditButton="True" UpdateText="Commit" />
<asp:CommandField ButtonType="Button"
DeleteText="" EditText="" InsertText="" NewText=""
SelectText="Export" UpdateText="" InsertVisible="False"
ShowCancelButton="False" ShowSelectButton="True" CancelText="" />
<asp:BoundField DataField="CME_Event_Track_ID" HeaderText="CME Track ID" SortExpression="CME_Event_Track_ID" />
<asp:BoundField DataField="Event_Type" HeaderText="Event Type" SortExpression="Event_Type" Visible="False" />
<asp:BoundField DataField="Event_Name" HeaderText="Event Name" SortExpression="Event_Name" />
<asp:BoundField DataField="Event_Desc" HeaderText="Event Desc" SortExpression="Event_Desc" />
<asp:BoundField DataField="EventDate" HeaderText="Event Date" SortExpression="EventDate" />
<asp:BoundField DataField="EventCount" HeaderText="Attendance Count" SortExpression="EventCount" />
<asp:BoundField DataField="MissingRFID" HeaderText="Number Of Missing RFID" SortExpression="MissingRFID" />

<footerstyle backcolor="#CCCCCC">
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<emptydatatemplate>
 

<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
JoCodes 18-Oct-13 1:06am    
Can you remove the GridView1.DataBind(); from the GridView1_RowEditing event since it rebinds the original values again.

1 solution

Try the below link which solves a similar problem

http://stackoverflow.com/questions/8529294/editing-gridview-not-working-after-filter[^]

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900