[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with Empty Datagrid

Posted on 2005-04-28
9
Medium Priority
?
514 Views
Last Modified: 2008-02-01
I am using asp.net with VB and need to have a datagrid used on the page which allows a  dropdownbox and a text filed to be used to populate the datagrid. The Customer enters that page, sees only the dropdownlist and the text field and the submit button, once the form is submitted, the data goes throw a calculation and populates the datagrid. The problem is that the datagrid is initially empty and the dropdown and text field are in the footer and I cannot get them to display.
I think I need to add an empty row to allow the datagrid to be visible.

Any help is always appreciated.

CJSantora

ASPX code:
sp:datagrid id="dgDefinitions" runat="server" Font-Names="Verdana" Font-Size="12px" Width="369px" CssClass="UsersGrid" GridLines="Vertical" BorderWidth="1px" CellPadding="3" BackColor="White" ForeColor="Black" BorderColor="#999999" AutoGenerateColumns="False" BorderStyle="Solid" Showfooter="True" OnItemCommand="doInsert">
                                                                                          <SelectedItemStyle Font-Bold="True" ForeColor="White" CssClass="UsersGrid" BackColor="#000099"></SelectedItemStyle>
                                                                                          <EditItemStyle CssClass="UsersGrid" BackColor="#CCCC99"></EditItemStyle>
                                                                                          <AlternatingItemStyle CssClass="UsersGrid" BackColor="#CCCCCC"></AlternatingItemStyle>
                                                                                          <HeaderStyle Font-Bold="True" ForeColor="White" CssClass="UsersGridHdr" BackColor="Black"></HeaderStyle>
                                                                                          <FooterStyle CssClass="UsersGrid" BackColor="#CCCCCC"></FooterStyle>
                                                                                          <Columns>
                                                                                                <asp:BoundColumn Visible="False" DataField="DID"></asp:BoundColumn>
                                                                                                <asp:TemplateColumn HeaderText="Job">
                                                                                                      <EditItemTemplate>
                                                                                                            <asp:TextBox ID="JobName" Columns="4" Runat="Server" />
                                                                                                      </EditItemTemplate>
                                                                                                </asp:TemplateColumn>
                                                                                                <asp:TemplateColumn HeaderText="Job Definition">
                                                                                                      <FooterTemplate>
                                                                                                            <asp:DropDownList ID="cboJobDefinition" Columns="20" Runat="Server" DataValueField="DID" DataTextField="JobDefinition" DataSource="<%# GetJobs() %>" />
                                                                                                      </FooterTemplate>
                                                                                                      <EditItemTemplate>
                                                                                                            <asp:DropDownList ID="JobDefinition" Columns="20" Runat="Server" />
                                                                                                      </EditItemTemplate>
                                                                                                </asp:TemplateColumn>
                                                                                                <asp:TemplateColumn HeaderText="Qty">
                                                                                                      <FooterTemplate>
                                                                                                            <asp:TextBox ID="add_Qty" Columns="2" Runat="Server" />
                                                                                                      </FooterTemplate>
                                                                                                      <EditItemTemplate>
                                                                                                            <asp:TextBox ID="txtQty" Columns="2" Text='' Runat="server" />
                                                                                                      </EditItemTemplate>
                                                                                                </asp:TemplateColumn>
                                                                                                <asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                                                                                                <asp:TemplateColumn HeaderText="Delete">
                                                                                                      <ItemTemplate>
                                                                                                            <asp:Button CommandName="Delete" Text="Delete" ID="cmdDel" Runat="server" />
                                                                                                      </ItemTemplate>
                                                                                                      <FooterTemplate>
                                                                                                            <asp:Button CommandName="Insert" Text="Add" ID="cmdAdd" Runat="server" />
                                                                                                      </FooterTemplate>
                                                                                                </asp:TemplateColumn>
                                                                                          </Columns>
                                                                                          <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"></PagerStyle>
                                                                                    </asp:datagrid>

0
Comment
Question by:CJSantora
  • 4
  • 4
9 Comments
 
LVL 9

Expert Comment

by:BurntSky
ID: 13889724
You're exactly right.  DataGrids don't show up on the page until they are bound.  You can get around this by binding an empty DataSet to the DataGrid.
0
 

Author Comment

by:CJSantora
ID: 13889883
thank you for the confirmation, but I thin k that is what I am having the probelm with. Is there  a chance I can get an example.
0
 
LVL 9

Accepted Solution

by:
BurntSky earned 2000 total points
ID: 13890081
Let me make sure I understand the problem correctly.  You have a DropDownList and a TextBox in the footer of your DataGrid.  Entering values into these controls and pressing a button binds data to the DataGrid.  However, because the DataGrid isn't initially bound, the footer, and therefore the controls in it, are not visible.  Does that sound about right?

My first suggestion would really be to put the controls outside the DataGrid.  If you absolutely have to have controls in the DataGrid's footer after it's been filled, you can create two sets of controls (one for initialization outside the DataGrid, and another set inside the DataGrid's footer) and then hide/show the controls as needed.

If that's not a viable option, try just creating a blank DataSet and binding it to the DataGrid the first time the page is loaded.

DataSet emptyDS = new DataSet();
this.dgDefinitions.DataSource = emptyDS;
this.dgDefinitions.DataBind();

That should work, however it might say certain columns/fields could not be found when you bind it.  If it does, you can manually add the columns to the DataSet's first DataTable, or you can create a strongly typed DataSet for the data you're binding, or you can execute a SQL command/s'proc to return 0 rows (or 1+ row(s) and just delete the unneeded ones.)

But, like I said, the best solution is to put the controls outside the DataGrid.  The second way is just a hack and is not very good application design.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 14

Expert Comment

by:puranik_p
ID: 13892113
@CJSantora, if your columns are autobound, the header and footer also diappear if its doesn't have any data.
Try setting the columns at design time.
0
 

Author Comment

by:CJSantora
ID: 13894908
Thanks for everyones input I decided to take the advise of BurntSky - however I am still havign problems, I moved the controls outside of the datagrid, but when I try to capture the data and bind it to the grid, I get no results, the other issue, is how to keep track of the number of rows in the grid, the user will be adding multiple entries, so how do I tell it to always add a new row. I am exclosing the code and would apprecite some addtional help with this onw.

DATAGRID
<asp:datagrid id="dgDefinitions" runat="server" Font-Names="Verdana" Font-Size="12px" Width="369px" CssClass="UsersGrid" GridLines="Vertical" BorderWidth="1px" CellPadding="3" BackColor="White" ForeColor="Black" BorderColor="#999999" AutoGenerateColumns="False" BorderStyle="Solid" Showfooter="True">
                                                                                          <SelectedItemStyle Font-Bold="True" ForeColor="White" CssClass="UsersGrid" BackColor="#000099"></SelectedItemStyle>
                                                                                          <EditItemStyle CssClass="UsersGrid" BackColor="#CCCC99"></EditItemStyle>
                                                                                          <AlternatingItemStyle CssClass="UsersGrid" BackColor="#CCCCCC"></AlternatingItemStyle>
                                                                                          <HeaderStyle Font-Bold="True" ForeColor="White" CssClass="UsersGridHdr" BackColor="Black"></HeaderStyle>
                                                                                          <FooterStyle CssClass="UsersGrid" BackColor="#CCCCCC"></FooterStyle>
                                                                                          <Columns>
                                                                                                <asp:BoundColumn Visible="False" DataField="DID"></asp:BoundColumn>
                                                                                                <asp:TemplateColumn HeaderText="Job">
                                                                                                      <ItemTemplate>
                                                                                                            <asp:TextBox ID="lblJobName" Columns="4" Runat="Server" />
                                                                                                      </ItemTemplate>                                                                                                
                                                                                                      <EditItemTemplate>
                                                                                                            <asp:TextBox ID="JobName" Columns="4" Runat="Server" />
                                                                                                      </EditItemTemplate>
                                                                                                </asp:TemplateColumn>
                                                                                                <asp:TemplateColumn HeaderText="Job Definition">
                                                                                                      <ItemTemplate>
                                                                                                            <asp:TextBox ID="lblJobDefinition" Columns="4" Runat="Server" />
                                                                                                      </ItemTemplate>                                                                                                      
                                                                                                      <EditItemTemplate>
                                                                                                            <asp:DropDownList ID="JobDefinition" Columns="20" Runat="Server" />
                                                                                                      </EditItemTemplate>
                                                                                                </asp:TemplateColumn>                                                                                          
                                                                                                <asp:TemplateColumn HeaderText="Qty">
                                                                                                      <ItemTemplate>
                                                                                                            <asp:TextBox ID="lblQty" Columns="4" Runat="Server" />
                                                                                                      </ItemTemplate>                                                                                                      
                                                                                                      <EditItemTemplate>
                                                                                                            <asp:TextBox ID="txtQty" Columns="2" Text='' Runat="server" />
                                                                                                      </EditItemTemplate>
                                                                                                </asp:TemplateColumn>
                                                                                                <asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                                                                                                <asp:TemplateColumn HeaderText="Delete">
                                                                                                      <ItemTemplate>
                                                                                                            <asp:Button CommandName="Delete" Text="Delete" ID="cmdDel" Runat="server" />
                                                                                                      </ItemTemplate>
                                                                                                </asp:TemplateColumn>
                                                                                          </Columns>
                                                                                          <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"></PagerStyle>
                                                                                    </asp:datagrid>


Code Behind:

    Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs)
        Dim DID As String
        Dim txtDID As TextBox

        Dim JobName As String
        Dim txtJobName As TextBox

        Dim Quantity As String
        Dim txtQty As TextBox

        Dim JobDefinition As String
        Dim txtJobDefinition As TextBox

        Dim oString As String

        'Read in the values of the TextBoxes
        txtDID = e.Item.FindControl("add_DID")
        txtJobName = e.Item.FindControl("lblJobName")
        txtQty = e.Item.FindControl("lblQty")
        txtJobDefinition = e.Item.FindControl("lblJobDefinition")

        dgDefinitions.EditItemIndex = -1
        dgDefinitions.DataBind()
    End Sub

0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13898308
I'm not sure I understand that last question.  Could you try explaining a little more?

Are you trying to use the DataGrid to add new entries to the database?
0
 

Author Comment

by:CJSantora
ID: 13900351
Thanks for the response.

Although that will be the final phase to this feature, we are not there yet. Currently I moved a dropdownlist and a text field outside the datagrid. The enduser is making a selection from the dropdownlist and entering a value in the text box and clicking a submit button. OnSubmit the row is added to the datagrid and this is working, however the values are not appearing. The current code behind is below, I am attempting to use a session on page load and allow the end user to add, and delte entries to the session, simliar to a shopping cart concept and then bind the session to the datagrid:

  Private Sub CreateSession()
        'If the DataTable is already stored in the Web form's default
        ' HttpSessionState variable, then don't recreate the DataTable.
        If Session("data") Is Nothing Then
            ' Add columns to the DataTable.
            dt.Columns.Add(New DataColumn("DID"))
            dt.Columns.Add(New DataColumn("JobDefinition"))
            dt.Columns.Add(New DataColumn("Quantity"))

            ' Store the DataTable in the Session variable so it can be
            ' accessed again later.
            Session("data") = dt

            ' Use the table to create a DataView, because the DataGrid
            ' can only bind to a data source that implements IEnumerable.
            dv = New DataView(dt)

            ' Set the DataView as the data source, and bind it to the DataGrid.
            dgDefinitions.DataSource = dv
            dgDefinitions.DataBind()
        End If

    End Sub

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        ' Add the items selected in ListBox1 to DataGrid1.
        Dim item As ListItem
        For Each item In cboDefinitions.Items
            If item.Selected Then
                ' Add the item to the DataGrid.
                ' First, get the DataTable from the Session variable.
                dt = CType(Session("data"), DataTable)

                If Not (dt Is Nothing) Then
                    ' Create a new DataRow in the DataTable.
                    Dim dr As DataRow
                    dr = dt.NewRow()
                    ' Add the item's value to the new DataRow.
                    dr("DID") = cboDefinitions.SelectedItem.Value
                    dr("JobDefinition") = cboDefinitions.SelectedItem.Text
                    dr("Quantity") = txtQuantity.Text
                    dt.Rows.Add(dr)

                    Response.Write("Date= " & cboDefinitions.SelectedItem.Value & " " & cboDefinitions.SelectedItem.Text & " " & txtQuantity.Text & " " & dr("DID"))
                    ' Rebind the data to DataGrid1.
                    dv = New DataView(dt)
                    dgDefinitions.DataSource = dv
                    dgDefinitions.DataBind()
                End If
            End If
        Next item
    End Sub


the code for the aspx pageis below:

<asp:datagrid id="dgDefinitions" runat="server" Font-Names="Verdana" Font-Size="12px" Width="369px" CssClass="UsersGrid" GridLines="Vertical" BorderWidth="1px" CellPadding="3" BackColor="White" ForeColor="Black" BorderColor="#999999" AutoGenerateColumns="False" BorderStyle="Solid" Showfooter="True">
                                                                           <SelectedItemStyle Font-Bold="True" ForeColor="White" CssClass="UsersGrid" BackColor="#000099"></SelectedItemStyle>
                                                                           <EditItemStyle CssClass="UsersGrid" BackColor="#CCCC99"></EditItemStyle>
                                                                           <AlternatingItemStyle CssClass="UsersGrid" BackColor="#CCCCCC"></AlternatingItemStyle>
                                                                           <HeaderStyle Font-Bold="True" ForeColor="White" CssClass="UsersGridHdr" BackColor="Black"></HeaderStyle>
                                                                           <FooterStyle CssClass="UsersGrid" BackColor="#CCCCCC"></FooterStyle>
                                                                           <Columns>
                                                                                <asp:BoundColumn Visible="False" DataField="DID"></asp:BoundColumn>
                                                                                <asp:TemplateColumn HeaderText="Job">
                                                                                     <ItemTemplate>
                                                                                          <asp:TextBox ID="lblJobName" Columns="4" Runat="Server" />
                                                                                     </ItemTemplate>                                                                                
                                                                                     <EditItemTemplate>
                                                                                          <asp:TextBox ID="JobName" Columns="4" Runat="Server" />
                                                                                     </EditItemTemplate>
                                                                                </asp:TemplateColumn>
                                                                                <asp:TemplateColumn HeaderText="Job Definition">
                                                                                     <ItemTemplate>
                                                                                          <asp:TextBox ID="lblJobDefinition" Columns="4" Runat="Server" />
                                                                                     </ItemTemplate>                                                                                    
                                                                                     <EditItemTemplate>
                                                                                          <asp:DropDownList ID="JobDefinition" Columns="20" Runat="Server" />
                                                                                     </EditItemTemplate>
                                                                                </asp:TemplateColumn>                                                                          
                                                                                <asp:TemplateColumn HeaderText="Qty">
                                                                                     <ItemTemplate>
                                                                                          <asp:TextBox ID="lblQty" Columns="4" Runat="Server" />
                                                                                     </ItemTemplate>                                                                                    
                                                                                     <EditItemTemplate>
                                                                                          <asp:TextBox ID="txtQty" Columns="2" Text='' Runat="server" />
                                                                                     </EditItemTemplate>
                                                                                </asp:TemplateColumn>
                                                                                <asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                                                                                <asp:TemplateColumn HeaderText="Delete">
                                                                                     <ItemTemplate>
                                                                                          <asp:Button CommandName="Delete" Text="Delete" ID="cmdDel" Runat="server" />
                                                                                     </ItemTemplate>
                                                                                </asp:TemplateColumn>
                                                                           </Columns>
                                                                           <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"></PagerStyle>
                                                                      </asp:datagrid>


I appreciate your help, I am already past my completion date on this project.

CJSantora
0
 
LVL 9

Expert Comment

by:BurntSky
ID: 13901311
Of course the values aren't showing up.  Theres no place in the DataGrid that is bound to them.  Change your columns to this:

<asp:TemplateColumn HeaderText="Job">
    <ItemTemplate>
        <asp:label id="lblJobName" runat="server"><%# DataBinder.Eval(Container.DataItem, "JobDefinition") %></asp:label>
    </ItemTemplate>                                                                                
    <EditItemTemplate>
        <asp:TextBox ID="JobName" Columns="4" Runat="Server" />
    </EditItemTemplate>
</asp:TemplateColumn>
0
 

Author Comment

by:CJSantora
ID: 13909295
I appreciate your help and patience, I guess I am ready for a vacation.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question