0

Why doesn't this grid appear on my web page? I am populating the drop down list with static items and the user will fill in the text boxes. I shouldn't need to do a data bind or to have a data source (I don't think). Right?

My goal is this: Easiest way to add multiple rows of data in ASP.NET Web Forms

        <asp:GridView ID="gvPurchaseDetails" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="true">
            <AlternatingRowStyle BackColor="Gainsboro" />
            <Columns>
                <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                        <asp:DropDownList runat="server">
                            <asp:ListItem Selected="True" Value="72594206916">Heart Pndnt Necklace (72594206916)</asp:ListItem>
                            <asp:ListItem Selected="True" Value="72594206916">Heart Pndnt Necklace (72594206916)</asp:ListItem>
                            <asp:ListItem Selected="True" Value="72594206916">Heart Pndnt Necklace (72594206916)</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                        <asp:TextBox ID="txtQuantity" runat="server" Width="25px" MaxLength="2" />
                    </ItemTemplate>                            
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Purchase Date">
                    <ItemTemplate>
                        <asp:TextBox ID="txtPurchaseDate" runat="server" Width="25px" />
                    </ItemTemplate>                            
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Purchase City">
                    <ItemTemplate>
                        <asp:TextBox ID="txtPurchaseCity" runat="server" Width="25px" />
                    </ItemTemplate>                            
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Purchase State">
                    <ItemTemplate>
                        <asp:TextBox ID="txtPurchaseState" runat="server" Width="25px" MaxLength="2" />
                    </ItemTemplate>                            
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#0000A9" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#000065" />
        </asp:GridView>
Community
  • 1
  • 1
birdus
  • 7,062
  • 17
  • 59
  • 89

2 Answers2

2

You need to set the data source:

if (!IsPostBack)
{
    gvPurchaseDetails.DataSource = GetPurchaseDetails(/* Get the data */);
    gvPurchaseDetails.DataBind();
}

UPDATE: You need to be using an EditItemTemplate:

<EditItemTemplate> 
    ...
</EditItemTemplate> 

And something like these properties added to your GridView declaration:

OnRowEditing="PurchaseGrid_RowEditing" 
OnRowCancelingEdit="PurchaseGrid_RowCancelingEdit" 
OnRowDeleting="PurchaseGrid_RowDeleting"
OnRowUpdating="PurchaseGrid_RowUpdating">
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • 1
    Oh yeah, I forgot empty GridViews are definitely invisible. –  Nov 07 '13 at 20:50
  • But I will populate the drop down list with static text (as in the example), and the text boxes will be filled in by the user. Why do I need a data source? Obviously, I'm missing something. – birdus Nov 07 '13 at 20:58
  • 1
    Sounds to me like you don't need a GridView in the first place. Just use a simple form. – IrishChieftain Nov 07 '13 at 21:17
  • Then how will the user be able to add an arbitrary number of rows? – birdus Nov 07 '13 at 21:19
  • Are you databasing input from the user? Not sure what you mean by "add an arbitrary number of rows". Typically, the values from these text boxes would form a single record in your database... – IrishChieftain Nov 07 '13 at 21:22
  • I will grab the values from the grid (all the values from all the rows the user enters) after the user posts back the form and do "stuff" with them. What I do with them is not relevant to this question (no flippancy intended). See this post for my goal: http://stackoverflow.com/questions/19842795/easiest-way-to-add-multiple-rows-of-data-in-asp-net-web-forms#19843415 – birdus Nov 07 '13 at 21:26
  • You do not need the overhead (and bloated ViewState) of a GridView for this; it serves no function. Simply lay out the controls with CSS, grab the values in the code-behind and work with them there. – IrishChieftain Nov 07 '13 at 21:28
  • Just read link to your other post. In Page_Load, store values in session state. Then in Pre-Render call your Add handler method to grab the new values from session state, dynamically create a new row, populate it, and do a rebind() to the grid ;-) – IrishChieftain Nov 07 '13 at 21:35
  • I'm not following. What relevance is session state if the grid isn't showing in the first place and the user is therefore not able to enter any values? I need the grid to show so the user can type stuff in so I can grab values to store. Moreover, the GridView does a lot for me (like adding and deleting rows) that would be a lot of work for me to do by hand, as you suggested). – birdus Nov 07 '13 at 21:39
  • GridView is not going to render initially without any data - catch 22. You could have the text boxes in the form as I said and when form is submitted toggle visibility of text boxes (use a panel), then bind an editable grid to the values inputted by the user. – IrishChieftain Nov 07 '13 at 21:44
1

I shouldn't need to do a data bind or to have a data source (I don't think). Right?

You need the data source. Even though the grid has controls with static data in them,it still needs its data source.

By default a, grid view won't render when no data is returned by the data source.To give you a visual hint in such cases , put an empty data template to show empty text when there are no records to show.

<asp:GridView ID="gvPurchaseDetails" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="true">
        <AlternatingRowStyle BackColor="Gainsboro" />
        <Columns>
            <asp:TemplateField HeaderText="Description">
                <ItemTemplate>
                    <asp:DropDownList runat="server">
                        <asp:ListItem Selected="True" Value="72594206916">Heart Pndnt Necklace (72594206916)</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
             <edititemtemplate>
           <asp:TextBox id="TextBoxEdit" runat="server" />
                 </edititemtemplate>
             <footertemplate>
                  <asp:TextBox id="TextBox1" runat="server" />
             </footertemplate>
            </asp:TemplateField>
                   </columns>
         <EmptyDataTemplate>
             Oops I have not data to show
            <asp:TextBox id="TextBoxNew" runat="server" />
         </EmptyDataTemplate>
           </asp:Gridview>

To use this grid view to enter data , that is when you make use of the footer template and set the show footer property to true on your grid view.Still the footer won't show where there is no data so you need to customize your grid view to do that.An easy way is to use the empty data template entering that initial first row.There are other elegant ways to manipulate the footer to show when there are no records returned by the data source. See an article here

Community
  • 1
  • 1
abidmix
  • 1,748
  • 1
  • 16
  • 26