I am trying to view/add/update/delete records with textboxes in same gridview. Only save button will be there at the end of page.
I have an ASP.NET gridview with 5 textboxes in every row. Page size of gridview is 20 records on each page. I have enabled sorting on every column. When the gridview is getting populated, then we are getting 28 filled records and 12 empty records.
When I hit sorting in asc mode on any column, it shows the blank rows earlier than the records with data. I want to ignore those blank records while sorting.
I tried using a custom onsorting method on gridview but it’s not working.
aspx code
<asp:GridView runat="server" ID="gvDetails" AllowPaging="true" PageSize="20" Allow AutoGenerateColumns="false" DataKeyNames="ProdId" OnPageIndexChanging="gvDetails_PageIndexChanging" OnSorting="gvDetails_OnSorting">
<HeaderStyle CssClass="headerstyle" />
<Columns>
<asp:TemplateField HeaderText="Product Name" SortExpression="ProdName">
<ItemTemplate>
<asp:TextBox ID="txtProductName" runat="server" Text='<%# Eval("ProdName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="ProdDesc">
<ItemTemplate>
<asp:TextBox ID="txtProdDesc" runat="server" Text='<%# Eval("ProdDesc") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" SortExpression="ProdPrice">
<ItemTemplate>
<asp:TextBox ID="txtProdPrice" runat="server" Text='<%# Eval("ProdPrice") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="ProdCat">
<ItemTemplate>
<asp:TextBox ID="txtProdCat" runat="server" Text='<%# Eval("ProdCat") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:CheckBox ID="chkDelete" runat="server" Checked='<%# Eval("IsDelete") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected DataTable BindGridview()
{
DataSet ds = new DataSet();
using (SqlConnection con = new SqlConnection("#"))
{
con.Open();
SqlCommand cmd = new SqlCommand("crudoperations", con);
cmd.CommandType= CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@status","SELECT");
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
else {
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvDetails.DataSource = ds;
gvDetails.DataBind();
int columncount = gvDetails.Rows[0].Cells.Count;
gvDetails.Rows[0].Cells.Clear();
gvDetails.Rows[0].Cells.Add(new TableCell());
gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvDetails.Rows[0].Cells[0].Text = "No Records Found";
}
}
return ds.Tables[0];
}
public void gvDetails_OnSorting(Object sender, GridViewSortEventArgs e)
{
string sortingDirection = string.Empty;
if (direction == SortDirection.Ascending)
{
direction = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
direction = SortDirection.Ascending;
sortingDirection = "Asc";
}
DataView sortedView = new DataView(BindGridView());
sortedView.Sort = e.SortExpression + " " + sortingDirection;
gvDetails.DataSource = sortedView;
gvDetails.DataBind();
}
2
Answers
Why don’t use the property "RowFilter" of the dataview: sortedView.RowFilter = "FilterExpression" ?
You have to filter the dataview before binding it
Something like this:
sortedView.RowFilter = e.SortExpression + "<> ''"
gvDetails.DataSource = sortedView;
gvDetails.DataBind();
Then I think the only way is to transfer the empty rows into a temporary table.
After that you make the sort on the datatable.
then, after the sort on the dataview that contains only filled rows, insert all empty rows from the temporary table at the end or whereever you want