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:

Troubleshooting
Research
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.

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

 <tr>
          <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" />
                  <Columns>
                      <asp:TemplateField ShowHeader="False">
                          <ItemTemplate>
                              <asp:Label ID="DefaultWorkDaysID" runat="server" 
                                  Text='<%# Bind("DefaultWorkDaysID") %>' Visible="False" />
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField ShowHeader="False">
                          <ItemTemplate>
                              <asp:Label ID="DefaultID" runat="server" Text='<%# Bind("DefaultID") %>' 
                                  Visible="False" />
                          </ItemTemplate>
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Day of Week">
                          <ItemTemplate>
                              <asp:DropDownList ID="DDDOW" runat="server" 
                                  DataTextField='<%# Bind("WorkDay") %>' DataValueField='<%# Bind("WorkDay") %>'>
                                  <asp:ListItem>Select...</asp:ListItem>
                                  <asp:ListItem>Sunday</asp:ListItem>
                                  <asp:ListItem>Monday</asp:ListItem>
                                  <asp:ListItem>Tuesday</asp:ListItem>
                                  <asp:ListItem>Wednesday</asp:ListItem>
                                  <asp:ListItem>Thursday</asp:ListItem>
                                  <asp:ListItem>Friday</asp:ListItem>
                                  <asp:ListItem>Saturday</asp:ListItem>
                              </asp:DropDownList>
                              <asp:ListSearchExtender ID="ListSearchExtender1" runat="server" 
                                  TargetControlID="DDDOW">
                              </asp:ListSearchExtender>
                          </ItemTemplate>
                          <ControlStyle BorderStyle="None" Width="100%" />
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Start Time">
                          <ItemTemplate>
                              <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">
                              </asp:MaskedEditExtender>
                          </ItemTemplate>
                          <ControlStyle BorderStyle="None" Width="100%" />
                          <HeaderStyle Width="24%" />
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="End Time">
                          <FooterTemplate>
                              <asp:LinkButton ID="lnkDefaultNewRow" runat="server" Font-Bold="False" 
                                  Font-Size="9pt" OnClick="lnkAddNewRow_Click" >Add New Row</asp:LinkButton>
                          </FooterTemplate>
                          <ItemTemplate>
                              <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">
                              </asp:MaskedEditExtender>
                          </ItemTemplate>
                          <ControlStyle BorderStyle="None" Width="100%" />
                          <HeaderStyle Width="24%" />
                      </asp:TemplateField>
                  </Columns>
                  <HeaderStyle BackColor="DarkBlue" Font-Size="9pt" ForeColor="White" />
              </asp:GridView>
               
              
                                          
              <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">
                  <DeleteParameters>
                      <asp:Parameter Name="DefaultWorkDaysID" Type="Int32" />
                  </DeleteParameters>
                  <InsertParameters>
                      <asp:Parameter Name="DefaultID" Type="Int32" />
                      <asp:Parameter Name="WorkDay" Type="String" />
                      <asp:Parameter Name="StartTime" Type="Int32" />
                      <asp:Parameter Name="EndTime" Type="Int32" />
                  </InsertParameters>
                  <SelectParameters>
                      <asp:ControlParameter ControlID="txtDefaultID" Name="DefaultID" 
                          PropertyName="Text" Type="Int32" />
                  </SelectParameters>
                  <UpdateParameters>
                      <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" />
                  </UpdateParameters>
              </asp:SqlDataSource>
               
              
                                          
        </td>
</tr>
ASKER CERTIFIED SOLUTION
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.

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