Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Gridview check linqDataSource before binding

Avatar of Sheritlw
SheritlwFlag for United States of America asked on
ASP.NETVisual Basic.NET
1 Comment1 Solution571 ViewsLast Modified:
I have been trying to find a solution for a while regarding the gridviews.

I am using Linq to Sql, but can use something else for the grids.

What I need to do, is check my datasource to see if there are any records, if there are, bind my grid and add a row.
If the datasource is empty then I need to create a fake row so that the header will show.

I have tried multiple ways to implement this, including trying the listview, with no luck.

I have attached my latest attempt, hoping that someone can help me so I can move on with this project.

In this example I am using sqldatasource, but I would prefer to use linq to sql.

          <td align="right" colspan="2">
              <asp:GridView ID="grdDefaults" runat="server" AllowSorting="True" 
                  AutoGenerateColumns="False" AutoGenerateDeleteButton="True" 
                  AutoGenerateEditButton="True" BorderColor="LightGray" Font-Size="8pt" 
                  ShowFooter="True" Width="100%" DataSourceID="sqldsWorkDays">
                  <AlternatingRowStyle BackColor="#FFFFCC" />
                      <asp:TemplateField ShowHeader="False">
                              <asp:Label ID="DefaultWorkDaysID" runat="server" 
                                  Text='<%# Bind("DefaultWorkDaysID") %>' Visible="False" />
                      <asp:TemplateField ShowHeader="False">
                              <asp:Label ID="DefaultID" runat="server" Text='<%# Bind("DefaultID") %>' 
                                  Visible="False" />
                      <asp:TemplateField HeaderText="Day of Week">
                              <asp:DropDownList ID="DDDOW" runat="server" 
                                  DataTextField='<%# Bind("WorkDay") %>' DataValueField='<%# Bind("WorkDay") %>'>
                              <asp:ListSearchExtender ID="ListSearchExtender1" runat="server" 
                          <ControlStyle BorderStyle="None" Width="100%" />
                      <asp:TemplateField HeaderText="Start Time">
                              <asp:TextBox ID="txtStartTime" runat="server" Text='<%# Bind("StartTime") %>'></asp:TextBox>
                              <asp:MaskedEditExtender ID="MaskedEditExtender1" runat="server" 
                                  AcceptAMPM="true" Mask="99:99" MaskType="Time" TargetControlID="txtStartTime">
                          <ControlStyle BorderStyle="None" Width="100%" />
                          <HeaderStyle Width="24%" />
                      <asp:TemplateField HeaderText="End Time">
                              <asp:LinkButton ID="lnkDefaultNewRow" runat="server" Font-Bold="False" 
                                  Font-Size="9pt" OnClick="lnkAddNewRow_Click" >Add New Row</asp:LinkButton>
                              <asp:TextBox ID="txtEndTime" runat="server" Text='<%# Bind("EndTime") %>'></asp:TextBox>
                              <asp:MaskedEditExtender ID="MaskedEditExtender2" runat="server" 
                                  AcceptAMPM="true" Mask="99:99" MaskType="Time" TargetControlID="txtEndTime">
                          <ControlStyle BorderStyle="None" Width="100%" />
                          <HeaderStyle Width="24%" />
                  <HeaderStyle BackColor="DarkBlue" Font-Size="9pt" ForeColor="White" />
              <asp:SqlDataSource ID="sqldsWorkDays" runat="server" OnDataBinding="AddRowIfNecessary" 
                  ConnectionString="<%$ ConnectionStrings:SalonConnectionString %>" 
                  DeleteCommand="DELETE FROM [DefaultWorkDays] WHERE [DefaultWorkDaysID] = @DefaultWorkDaysID" 
                  InsertCommand="INSERT INTO [DefaultWorkDays] ([DefaultID], [WorkDay], [StartTime], [EndTime]) VALUES (@DefaultID, @WorkDay, @StartTime, @EndTime)" 
                  SelectCommand="SELECT * FROM [DefaultWorkDays] WHERE ([DefaultID] = @DefaultID)" 
                  UpdateCommand="UPDATE [DefaultWorkDays] SET [DefaultID] = @DefaultID, [WorkDay] = @WorkDay, [StartTime] = @StartTime, [EndTime] = @EndTime WHERE [DefaultWorkDaysID] = @DefaultWorkDaysID">
                      <asp:Parameter Name="DefaultWorkDaysID" Type="Int32" />
                      <asp:Parameter Name="DefaultID" Type="Int32" />
                      <asp:Parameter Name="WorkDay" Type="String" />
                      <asp:Parameter Name="StartTime" Type="Int32" />
                      <asp:Parameter Name="EndTime" Type="Int32" />
                      <asp:ControlParameter ControlID="txtDefaultID" Name="DefaultID" 
                          PropertyName="Text" Type="Int32" />
                      <asp:Parameter Name="DefaultID" Type="Int32" />
                      <asp:Parameter Name="WorkDay" Type="String" />
                      <asp:Parameter Name="StartTime" Type="Int32" />
                      <asp:Parameter Name="EndTime" Type="Int32" />
                      <asp:Parameter Name="DefaultWorkDaysID" Type="Int32" />
Avatar of Muhammad Zaman
Muhammad ZamanFlag of Pakistan imageSoftware Development Lead

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answers