Solved

Empty Datagrid

Posted on 2009-03-30
7
922 Views
Last Modified: 2012-05-06
How do i show and empty datagrid.

i need to show its header but the rows will be empty? Rows will be added dynamically

First thing First.
Can some one show me how to create a datagrid just to show its headers....i dont want to bind it to any db datasource

0
Comment
Question by:AsishRaj
  • 4
  • 3
7 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 500 total points
ID: 24023919
It turns out you need to do some kind of data binding to show the datagrid only with headers.

// aspx page

<asp:DataGrid ID="dgTest" runat="server" AutoGenerateColumns="false">

    <Columns>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 1 

            </HeaderTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 2

            </HeaderTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 3 

            </HeaderTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 4 

            </HeaderTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 5 

            </HeaderTemplate>

        </asp:TemplateColumn>

    </Columns>

</asp:DataGrid>
 

// code-behind page:

DataTable dt = new DataTable();

dt.Rows.Add(dt.NewRow());

dgTest.DataSource = dt;

dgTest.DataBind();

Open in new window

0
 
LVL 9

Author Comment

by:AsishRaj
ID: 24024286
yeap that work great.

One simple Question
Now how do i insert a new row when a button is clicked
0
 
LVL 27

Expert Comment

by:nmarun
ID: 24024307
>>'insert a new row'
Do you mean you need textboxes to show up so the user can add data?
or
Do you mean that the user has entered data through some other GUI and you want to that data in the grid?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Author Comment

by:AsishRaj
ID: 24024328
>>Do you mean you need textboxes to show up so the user can add data?

Yes. Finally i think i have met the real Guru
0
 
LVL 27

Expert Comment

by:nmarun
ID: 24024656
You can do that in a few ways. Using a panel containing the fields is one way:

<asp:DataGrid ID="dgTest" runat="server" AutoGenerateColumns="false">

    <Columns>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 1 

            </HeaderTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 2

            </HeaderTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 3 

            </HeaderTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 4 

            </HeaderTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <HeaderTemplate>

                Header 5 

            </HeaderTemplate>

        </asp:TemplateColumn>

    </Columns>

</asp:DataGrid>

<asp:Button ID="btnShowAdd" runat="server" onclick="btnShowAdd_Click" Text="Add" />

<br />

<asp:Panel ID="pnlTest" runat="server">

    <table>

        <tr>

            <td>

                <asp:TextBox ID="txtBox1" runat="server" Width="50px" />

            </td>

            <td>

                <asp:TextBox ID="txtBox2" runat="server" Width="50px" />

            </td>

            <td>

                <asp:TextBox ID="txtBox3" runat="server" Width="50px" />

            </td>

            <td>

                <asp:TextBox ID="txtBox4" runat="server" Width="50px" />

            </td>

            <td>

                <asp:TextBox ID="txtBox5" runat="server" Width="50px" />

            </td>

            <td>

                <asp:Button ID="btnSave" runat="server" Text="Save"/>

            </td>

        </tr>

    </table>

</asp:Panel>
 

// code-behind

protected void Page_Load(object sender, EventArgs e)

{

    if (!IsPostBack)

    {

        pnlTest.Visible = false;

        DataTable dt = new DataTable();

        dt.Rows.Add(dt.NewRow());

        dgTest.DataSource = dt;

        dgTest.DataBind();

    }

}
 

protected void btnShowAdd_Click(object sender, EventArgs e)

{

    pnlTest.Visible = true;

}

Open in new window

0
 
LVL 9

Author Comment

by:AsishRaj
ID: 24024694
can we insert a new row in the datagrid when the button is clicked.

this what i have so far

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            ' Generate rows and cells.           

            Dim dt As New DataTable()

            dt.Rows.Add(dt.NewRow())

            dgTest.DataSource = dt

            dgTest.DataBind()

        End If

    End Sub
 

    Protected Sub dgTest_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest.ItemCreated

        If e.Item.ItemType = ListItemType.EditItem Then

            Dim e1 As TextBox = DirectCast(e.Item.FindControl("TextBox3"), TextBox)

            e1.Width = Unit.Parse("75px")

            e1 = DirectCast(e.Item.FindControl("TextBox2"), TextBox)

            e1.Width = Unit.Parse("75px")

            e1 = DirectCast(e.Item.FindControl("TextBox1"), TextBox)

            e1.Width = Unit.Parse("75px")

        End If

    End Sub

 Sub doEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

        dgTest.ShowFooter = False

        dgTest.EditItemIndex = e.Item.ItemIndex

        BindData()

    End Sub
 

    Sub doCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

        dgTest.ShowFooter = True

        dgTest.EditItemIndex = -1

        BindData()

    End Sub
 

    Sub doInsert(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

'add a new row to datagrid
 

    End Sub
 

aspx
 

<asp:DataGrid ID="dgTest" runat="server" AutoGenerateColumns="false" BorderColor="White" BorderStyle="Solid" BorderWidth="2px" 

    CellPadding="3" CellSpacing="1" Font-Names="Tahoma" Font-Size="Small" 

    HorizontalAlign="Center"  ShowFooter="True"

    OnEditCommand ="doEdit" OnCancelCommand="doCancel" >

    <FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" 

        Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />

    <Columns>

        <asp:TemplateColumn>

            <EditItemTemplate>

                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Shop" 

                    DataTextField="ShopName" DataValueField="ShopCode">

                </asp:DropDownList>

                <asp:SqlDataSource ID="Shop" runat="server" 

                    ConnectionString="<%$ ConnectionStrings:eTFSConnectionString %>" 

                    SelectCommand="SELECT * FROM [DEFShops]"></asp:SqlDataSource>

            </EditItemTemplate>

            <HeaderTemplate>

                Shop Name 

            </HeaderTemplate>

            <ItemTemplate>

                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

            </ItemTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <EditItemTemplate>

                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

            </EditItemTemplate>

            <HeaderTemplate>

                Docket Number

            </HeaderTemplate>

            <ItemTemplate>

                <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>

            </ItemTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <EditItemTemplate>

                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

            </EditItemTemplate>

            <HeaderTemplate>

                Docket Total

            </HeaderTemplate>

            <ItemTemplate>

                <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>

            </ItemTemplate>

        </asp:TemplateColumn>

        <asp:TemplateColumn>

            <EditItemTemplate>

                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

            </EditItemTemplate>

            <HeaderTemplate>

                Docket Date

            </HeaderTemplate>

            <ItemTemplate>

                <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>

            </ItemTemplate>

        </asp:TemplateColumn>

         <asp:EditCommandColumn

	     ButtonType="PushButton" UpdateText="Update" CancelText="Cancel"

	     EditText="Edit" HeaderText="Edit">

      </asp:EditCommandColumn>

        <asp:TemplateColumn HeaderText="Delete">

        <FooterTemplate>

          <asp:Button CommandName="Insert" Text="Add" ID="btnAdd" Runat="server" OnClick="AddRow" />

        </FooterTemplate>

        <ItemTemplate>

          <asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server" />

        </ItemTemplate>

      </asp:TemplateColumn>
 

    </Columns>

    <HeaderStyle BackColor="#3366CC" Font-Bold="False" Font-Italic="False" 

                                                    Font-Names="Tahoma" Font-Overline="False" Font-Size="Small" 

                                                    Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" 

                                                    ForeColor="White" />

</asp:DataGrid>

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
ID: 24024967
Ashish,

I'd like to keep issues small and simple. The previous request of yours seems to be going out of the scope of this ticket. It's just easier for someone who sees this issue to understand what was requested and how it was solved when they see tickets with predefined scope.

Please open another ticket, explain what it is that you are doing there and I'm sure you'll find many other 'Gurus' in this discussion site.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP:DataGrid and how to bind to a BoundColumn 8 38
Editing XSL files, 2 44
Re-position sub-options beneath the TAB 7 79
How do ASP.NET and MVC work together? 4 29
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now