Answered by:
Blank Insert Cells not showing

Question
-
User1216627406 posted
Greetings experts,
I am using GridView CRUD operations to insert, update, delete and display records.
Initially, there will be no records on the database. So, when a user loads the form, there will be blank cells for user to enter records.
So far, for some reason, the blank cells are not being visible. Users cannot enter records unless there are cells to enter data into.
Does any one have any ideas what I could be doing wrong?
Many thanks in advance. Here is the code I am using so far.
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>XPHIAS Client Management System</title> <style type="text/css"> table.gridview td { border: 1px solid #000; } .gridheader { background-image: url("images/bgBack.png"); background-repeat: repeat-x; } </style> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <div> <asp:GridView ID="gvSpeaker" runat="server" AutoGenerateColumns="False" DataKeyNames="SpeakerID" onrowcancelingedit="gvSpeaker_RowCancelingEdit" onrowediting="gvSpeaker_RowEditing" onrowdeleting="gvSpeaker_RowDeleting" onrowupdating="gvSpeaker_RowUpdating" GridLines="None" AllowPaging="true" CssClass="gridview gridheader" AlternatingRowStyle-CssClass="alt" Width="100%" CellPadding="3" PageSize="5" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2"> <Columns> <asp:TemplateField HeaderText="S.No." ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <%#Container.DataItemIndex+1%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Speaker Name"> <ItemTemplate> <%# Eval("SpeakerName")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtspeakername" runat="server" Text='<%#Eval("SpeakerName") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtcSpeakerName1" runat="server" Width="150px"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Ministry Name"> <ItemTemplate> <%#Eval("MinistryName")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtministryname" runat="server" Text='<%#Eval("MinistryName") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtMinistryName1" runat="server" Width="150px"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Client Name"> <ItemTemplate> <%# Eval("ClientName") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtclientname" runat="server" Text='<%#Eval("ClientName") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtcClientName1" Width="100px" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <%#Eval("Email")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtemail" runat="server" Text='<%#Eval("Email") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtcEmail1" Width="150px" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Date Added"> <ItemTemplate> <%#Eval("dateAdded")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtdateadded" runat="server" Text='<%#Eval("dateAdded") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtcDateAdded1" Width="150px" CssClass="clientDate" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Website URL"> <ItemTemplate> <%#Eval("WebsiteURL")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtwebsiteurl" runat="server" Text='<%#Eval("WebsiteURL") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtcWebsiteURL1" Width="150px" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="true" ButtonType ="Image" EditImageUrl="Image/edit.png" UpdateImageUrl="Image/accept.png" CancelImageUrl="Image/cancel.png" HeaderText="Edit" /> <asp:CommandField ShowDeleteButton="true" ButtonType="Image" DeleteImageUrl="Image/delete.png" HeaderText="Delete" /> </Columns> <RowStyle BackColor="#BDBDBD" ForeColor="#3C3C3C" /> <FooterStyle BackColor="#E0E0E0" ForeColor="#3C3C3C" /> <PagerStyle BackColor="#2C3539" ForeColor="#FFFAF8" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#BDBDBD" Font-Bold="True" ForeColor="White" /> <HeaderStyle Font-Bold="True" ForeColor="#FFFAF8" /> <AlternatingRowStyle BackColor="#E0E0E0" /> </asp:GridView><br /> </div> </form> </body> </html> //C# using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Home : Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GetSpeakerDetail(); } } private SqlConnection GetConnection() { string connStr = ConfigurationManager.ConnectionStrings["XString"].ConnectionString; SqlConnection con = new SqlConnection(connStr); con.Open(); return con; } public void GetSpeakerDetail() { SqlConnection con = GetConnection(); SqlCommand cmd = new SqlCommand("_sp_GetSpeakerDetail", con); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); con.Close(); if (ds.Tables[0].Rows.Count > 0) { gvSpeaker.DataSource = ds; gvSpeaker.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); // if record not found then returning a blank table structure gvSpeaker.DataSource = ds; gvSpeaker.DataBind(); int columncount = gvSpeaker.Rows[0].Cells.Count; gvSpeaker.Rows[0].Cells.Clear(); gvSpeaker.Rows[0].Cells.Add(new TableCell()); gvSpeaker.Rows[0].Cells[0].ColumnSpan = columncount; gvSpeaker.Rows[0].Cells[0].Text = "No Records Found"; } } protected void gvSpeaker_RowDeleting(object sender, GridViewDeleteEventArgs e) { int id = Convert.ToInt32(gvSpeaker.DataKeys[e.RowIndex].Value.ToString()); SqlConnection con = GetConnection(); SqlCommand cmd = new SqlCommand("_sp_DeleteSpeaker", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Id", id); if (con.State == ConnectionState.Closed) { con.Open(); } cmd.ExecuteNonQuery(); GetSpeakerDetail(); } protected void gvSpeaker_RowEditing(object sender, GridViewEditEventArgs e) { gvSpeaker.EditIndex = e.NewEditIndex; GetSpeakerDetail(); } protected void gvSpeaker_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvSpeaker.EditIndex = -1; GetSpeakerDetail(); } protected void gvSpeaker_RowUpdating(object sender, GridViewUpdateEventArgs e) { int id = Convert.ToInt32(gvSpeaker.DataKeys[e.RowIndex].Value.ToString()); string speaker_name = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempname")).Text; string ministry_name = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text; string client_name = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text; string email = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text; string date_added = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text; string website_url = ((TextBox)gvSpeaker.Rows[e.RowIndex].FindControl("txtempcode")).Text; SqlConnection con = GetConnection(); if (con.State == ConnectionState.Closed) { con.Open(); } SqlCommand cmd = new SqlCommand("_sp_UpdateSpeaker", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Id ", id); cmd.Parameters.AddWithValue("@Speaker_Name ", speaker_name); cmd.Parameters.AddWithValue("@Ministry_Name ", ministry_name); cmd.Parameters.AddWithValue("@Client_Name ", client_name); cmd.Parameters.AddWithValue("@Email ", email); cmd.Parameters.AddWithValue("@date_Added ", date_added); cmd.Parameters.AddWithValue("@Website_URL ", website_url); cmd.ExecuteNonQuery(); con.Close(); gvSpeaker.EditIndex = -1;// no row in edit mode GetSpeakerDetail(); } }
Tuesday, May 11, 2021 3:08 PM
Answers
-
User-939850651 posted
Hi simflex,
When the GridView data source is empty, Footer will not be displayed by default.
You could try the EmptyDataTemplate already mentioned, or you could add empty row in the datasource and hide it to achieve the same effect. Something like this:
private void FillVendorGrid() { dataTable = new DataTable(); cmd.Connection = conn; cmd.CommandText = "SELECT * FROM Vendor"; ad = new SqlDataAdapter(cmd); ad.Fill(dataTable); if (dataTable.Rows.Count == 0) { DataRow row = dataTable.NewRow(); dataTable.Rows.Add(row); } ResultGridView.DataSource = dataTable; ResultGridView.DataBind(); }
protected void ResultGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[0].Text == " ") { e.Row.Visible = false; } } }
Result:
Hope this can help.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 13, 2021 7:36 AM
All replies
-
User475983607 posted
So far, for some reason, the blank cells are not being visible. Users cannot enter records unless there are cells to enter data into.
Does any one have any ideas what I could be doing wrong?
There's nothing wrong. You are describing exactly how a bound data control works. A result set is returned from a query and used to populate the GridView.
How does you design handle inserting new records? I assume you would follow the same process when the data source is empty.
A common GridView programming pattern is using the table footer for entering a new records. The GridView has an empty item template where you can add an input forum. This might work if the the GridView is not filtered.
Tuesday, May 11, 2021 3:30 PM -
User1216627406 posted
What I was trying to say was that I have this method which should display blank cells for entering new records.
There should be blank cells regardless of whether a record exists or not
EmptyDataText doesnt seem to do the trick for me.
This is supposed to but so far no luck with it:
public void GetSpeakerDetail() { SqlConnection con = GetConnection(); SqlCommand cmd = new SqlCommand("_sp_GetSpeakerDetail", con); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); con.Close(); if (ds.Tables[0].Rows.Count > 0) { gvSpeaker.DataSource = ds; gvSpeaker.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); // if record not found then returning a blank table structure gvSpeaker.DataSource = ds; gvSpeaker.DataBind(); int columncount = gvSpeaker.Rows[0].Cells.Count; gvSpeaker.Rows[0].Cells.Clear(); gvSpeaker.Rows[0].Cells.Add(new TableCell()); gvSpeaker.Rows[0].Cells[0].ColumnSpan = columncount; gvSpeaker.Rows[0].Cells[0].Text = "No Records Found"; } }
Tuesday, May 11, 2021 4:16 PM -
User475983607 posted
You're making a very simple solution overly complicated. What is your current process for adding a new record? Just use the same insert logic when the procedure returns an empty set.
Tuesday, May 11, 2021 6:10 PM -
User-1716253493 posted
There is empty data template in the grid. You can add some controls, i.e textboxes. You can also add formview etc. I think it's easier.
Tuesday, May 11, 2021 6:16 PM -
User1216627406 posted
You're making a very simple solution overly complicated.
You always say this and if that it is that simple and I am struggling, why not show some simple example?
In any case, I decided to use the code below and just add it below the closing gridview </GridView>
I knew this option is available but I didn't want to use it because it forces me to add new column headers.
Additionally, the formats and styles are always different from Gridview but for now, until I find something better, this will be just enough.<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse"> <tr> <td style="width: 150px"> Speaker Name:<br /> <asp:TextBox ID="txtSpeakerName" runat="server" Width="140" /> </td> <td style="width: 150px"> Ministry Name:<br /> <asp:TextBox ID="txtMinistryName" runat="server" Width="140" /> </td> <td style="width: 150px"> <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" /> </td> </tr> </table>
Tuesday, May 11, 2021 7:47 PM -
User475983607 posted
simflex
You always say this and if that it is that simple and I am struggling, why not show some simple example?I asked two times to explain how you are adding new records. You never answered the question. I cannot read your mind and guessing is a waste of time. I'm a fan of simple and straight forward. I would reuse the forum designed to insert records. The only time you insert a record is when the table has no records???
simflex
I knew this option is available but I didn't want to use it because it forces me to add new column headers.
Additionally, the formats and styles are always different from Gridview but for now, until I find something better, this will be just enough.You could have used the GridView footer which is very common. Anyway, you can copy the GridView styles to your table. Open dev tools and grab the styles.
Tuesday, May 11, 2021 8:16 PM -
User1216627406 posted
The only time you insert a record is when the table has no records???
No sir, what kind of question is that?
What I said was that if for instance you are using CRUD operations to design a system for a user and when the systems goes LIVE, the user has blank database to start with, then they would need to start entering data into the database depending on what they are trying to accomplish.
The sample that I used that posted, has some records and with existing records to display, the blank cells for inserting new records become visible.
However, if you delete those records to have a clean slate to begin with, then those blank cells for some reason are not visible.
My point is that those blank cells need to be visible whether there is an existing record to display or DB is blank.
I am able to design that table with the styles used for Gridview, I just couldn't the cells to align correctly with the gridview cells but that's ok - for now at least.
Tuesday, May 11, 2021 10:36 PM -
User-939850651 posted
Hi simflex,
simflex
My point is that those blank cells need to be visible whether there is an existing record to display or DB is blank.I think what you are describing is that the data in the table is gradually increased by user operations, and these blank cells are reserved space for new data to be filled, right?
When there are no records in the table, the GridView will show empty data. Would you consider guiding users to add new records to it? If this is the case, I think it is more important to design the logic of adding records to the table.
Best regards,
Xudong Peng
Wednesday, May 12, 2021 6:06 AM -
User1216627406 posted
Please look at the example on the link below:
Near the bottom of the page, you will see a screenshot of GridView with data (5 rows) and the new blank cells with Add New button.
I used that example but when there is no data populating the GridView from the database, those blank cells with Add New button are not visible.
I must be missing something.
Wednesday, May 12, 2021 4:41 PM -
User-939850651 posted
Hi simflex,
When the GridView data source is empty, Footer will not be displayed by default.
You could try the EmptyDataTemplate already mentioned, or you could add empty row in the datasource and hide it to achieve the same effect. Something like this:
private void FillVendorGrid() { dataTable = new DataTable(); cmd.Connection = conn; cmd.CommandText = "SELECT * FROM Vendor"; ad = new SqlDataAdapter(cmd); ad.Fill(dataTable); if (dataTable.Rows.Count == 0) { DataRow row = dataTable.NewRow(); dataTable.Rows.Add(row); } ResultGridView.DataSource = dataTable; ResultGridView.DataBind(); }
protected void ResultGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[0].Text == " ") { e.Row.Visible = false; } } }
Result:
Hope this can help.
Best regards,
Xudong Peng
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, May 13, 2021 7:36 AM -
User1216627406 posted
Thank you very much!
Friday, May 14, 2021 6:27 PM