Reputation: 7542
I hide a certain button that is in a gridview if certain conditions are met:
protected void storyGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int count = Int32.Parse(SqlInteraction.sqlQueryReturnString(conn => countNumberOfHoursBilled(conn, DataBinder.Eval(e.Row.DataItem, "PK_NonScrumStory").ToString())));
DateTime createdDate = DateTime.Parse(DataBinder.Eval(e.Row.DataItem, "CreatedDate").ToString());
if (count > 0 || createdDate < DateTime.Today)
{
Button btn = (Button)e.Row.FindControl("deleteButton");
btn.Visible = false;
}
else
{
Button btn = (Button)e.Row.FindControl("deleteButton");
btn.Visible = true;
}
}
}
THis works well, But I have noticed that when one of the conditions is met during use of the page the delete button does not change back and forth correctly unless I refresh the page.
I have tried to databind the gridview but it's not working;
protected void viewHoursGridView_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView gv = sender as GridView;
SqlDataSource6.DeleteParameters["setDailyPKDeleteParam"].DefaultValue = gv.DataKeys[e.RowIndex].Value.ToString();
storyGridView.DataBind();
}
So if I delete a row and count goes to zero, the button should be visible. But it won't become visible until i refresh the page.
How do I fix this?
Edit:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="
SELECT NSS.PK_NonScrumStory
,SCY.Catagory AS Catagory
,APN.AppName AS [Application]
,NSS.IncidentNumber AS IncidentNumber
,CASE WHEN (NSS.[Status] = 1) THEN 'Open' ELSE 'Closed' END AS [Status]
,NSS.EstimatedHours AS EstimatedHours
,NSS.[Description] AS [Description]
,NSS.CreateDate AS CreatedDate
FROM NonScrumStory NSS
LEFT JOIN SupportCatagory SCY ON NSS.CatagoryId = SCY.PK_SupportCatagory
LEFT JOIN [Application] APN ON NSS.ApplicationId = APN.PK_Application
WHERE NSS.Deleted = 0
AND NSS.UserId = @nonScrumStoryUser
ORDER BY CreatedDate DESC
">
<SelectParameters>
<asp:QueryStringParameter Name="nonScrumStoryUser" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
Upvotes: 0
Views: 143
Reputation: 928
You need to get the data again in RowDeleting event and then bind that data to the GridView. You essentially need to set the DataSource of GridView again and bind the data so you can see the updated data after deleting a row.
Update
After taking a look at the the SqlDataSource, you have not written a command for deleting the row when you click the Delete button. So the changes are not being pushed to the database and that's the reason your GridView is not seeing the change. You need to add a DeleteCommand to the SqlDataSource.
DeleteCommand="DELETE FROM [TableName] WHERE [ColumnName]=@[ParameterName];">
<DeleteParameters>
<asp:ControlParameter Name="[ParameterName]" ControlId="storyGridView" PropertyName="SelectedValue" />
</DeleteParameters>
The code might not be exactly syntactically correct, but this is to give you an idea. Take a look at this sample from MSDN.
Upvotes: 1