JDEE8297
asked on
Creating an editable datagrid in asp.net / vb.net, Internet Explorer 7.0
Greetings,
I know at the end of this I will be kicking myself, but right now I am having a brain fart and cannot for the life of me remember how to set this up.
I am trying to create an editable datagrid and here is the datagrid source:
<asp:datagrid id="dgItemsReturn" runat="server" showfooter="true" AutoGenerateColumns="false "
Width="80%" HeaderStyle-BackColor="#33 99FF" HeaderStyle-CssClass="btex twhite" HeaderStyle-VerticalAlign= "bottom"
HeaderStyle-HorizontalAlig n="center" FooterStyle-BackColor="#33 99FF" ItemStyle-BackColor="#FFFF CC" ItemStyle-CssClass="dtltex t"
ItemStyle-VerticalAlign="t op" AlternatingItemStyle-BackC olor="#FFF FFF" CellPadding="1" BorderColor="#CCCCCC"
BorderWidth="1">
<Columns>
<asp:TemplateColumn HeaderText="Id">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head erStyle>
<ItemTemplate>
<asp:label id="lblMerchReturnItemRecI d" runat="server" cssclass="rtext"></asp:lab el>
<asp:TextBox id="tbxRecStatus" visible="false" runat="server"></asp:TextB ox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="BV Item">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head erStyle>
<ItemTemplate>
<asp:TextBox id="tbxBvItem" cssclass="rtext" runat="server"></asp:TextB ox>
</ItemTemplate>
<footertemplate>
<asp:TextBox id="tbxNewBvItem" cssclass="rtext" runat="server"></asp:TextB ox>
</footertemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head erStyle>
<ItemTemplate>
<asp:TextBox ID="tbxBvItemDesc" cssclass="rtext" Runat="server"></asp:TextB ox>
</ItemTemplate>
<footertemplate>
<asp:TextBox id="tbxNewBvItemDesc" cssclass="rtext" runat="server"></asp:TextB ox>
</footertemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Qty">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head erStyle>
<ItemTemplate>
<asp:TextBox ID="tbxBvItemQty" cssclass="rtext" Runat="server"></asp:TextB ox>
</ItemTemplate>
<footertemplate>
<asp:TextBox id="tbxNewBvItemQty" cssclass="rtext" runat="server"></asp:TextB ox>
</footertemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Unit Price">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head erStyle>
<ItemTemplate>
<asp:TextBox ID="tbxBvItemUnitPrice" cssclass="rtext" Runat="server"></asp:TextB ox>
</ItemTemplate>
<footertemplate>
<asp:TextBox id="tbxNewBvItemUnitPrice" cssclass="rtext" runat="server"></asp:TextB ox>
</footertemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Total">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head erStyle>
<ItemTemplate>
<asp:label id="lblBvItemTotalCost" runat="server" cssclass="rtext"></asp:lab el>
</ItemTemplate>
<footertemplate>
<asp:LinkButton Runat="server" Text="Add" CommandName="AddItem" ID="lkbAddItem" NAME="Linkbutton1"></asp:L inkButton>
</footertemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
I plan to populate this datagrid from a database table (sql server 2000) and have the itemdatabound event all set up when the return resultset is bound to the datagrid. My problem is that if there is no items in the resultset, how do I get the get the grid to show up with the footer templates, so that the user can enter in new items.
Like I said at the start, probably will kick myself once I see the solution, but in the mean time if anyone can help out here, I would appreciate it. Thanks.
JD
I know at the end of this I will be kicking myself, but right now I am having a brain fart and cannot for the life of me remember how to set this up.
I am trying to create an editable datagrid and here is the datagrid source:
<asp:datagrid id="dgItemsReturn" runat="server" showfooter="true" AutoGenerateColumns="false
Width="80%" HeaderStyle-BackColor="#33
HeaderStyle-HorizontalAlig
ItemStyle-VerticalAlign="t
BorderWidth="1">
<Columns>
<asp:TemplateColumn HeaderText="Id">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head
<ItemTemplate>
<asp:label id="lblMerchReturnItemRecI
<asp:TextBox id="tbxRecStatus" visible="false" runat="server"></asp:TextB
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="BV Item">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head
<ItemTemplate>
<asp:TextBox id="tbxBvItem" cssclass="rtext" runat="server"></asp:TextB
</ItemTemplate>
<footertemplate>
<asp:TextBox id="tbxNewBvItem" cssclass="rtext" runat="server"></asp:TextB
</footertemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head
<ItemTemplate>
<asp:TextBox ID="tbxBvItemDesc" cssclass="rtext" Runat="server"></asp:TextB
</ItemTemplate>
<footertemplate>
<asp:TextBox id="tbxNewBvItemDesc" cssclass="rtext" runat="server"></asp:TextB
</footertemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Qty">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head
<ItemTemplate>
<asp:TextBox ID="tbxBvItemQty" cssclass="rtext" Runat="server"></asp:TextB
</ItemTemplate>
<footertemplate>
<asp:TextBox id="tbxNewBvItemQty" cssclass="rtext" runat="server"></asp:TextB
</footertemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Unit Price">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head
<ItemTemplate>
<asp:TextBox ID="tbxBvItemUnitPrice" cssclass="rtext" Runat="server"></asp:TextB
</ItemTemplate>
<footertemplate>
<asp:TextBox id="tbxNewBvItemUnitPrice"
</footertemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Total">
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Top"></Head
<ItemTemplate>
<asp:label id="lblBvItemTotalCost" runat="server" cssclass="rtext"></asp:lab
</ItemTemplate>
<footertemplate>
<asp:LinkButton Runat="server" Text="Add" CommandName="AddItem" ID="lkbAddItem" NAME="Linkbutton1"></asp:L
</footertemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
I plan to populate this datagrid from a database table (sql server 2000) and have the itemdatabound event all set up when the return resultset is bound to the datagrid. My problem is that if there is no items in the resultset, how do I get the get the grid to show up with the footer templates, so that the user can enter in new items.
Like I said at the start, probably will kick myself once I see the solution, but in the mean time if anyone can help out here, I would appreciate it. Thanks.
JD
ASKER
I think I am going to need a bigger foot ;)
I will try this and see if it does the job for me, I don't know c# very well, but what I am able to figure out what the code is doing. So it is not too bad. Thanks again. If this works, then I will let you know.
I will try this and see if it does the job for me, I don't know c# very well, but what I am able to figure out what the code is doing. So it is not too bad. Thanks again. If this works, then I will let you know.
ASKER
actually, I guess I am not sure how this can work, and it is probably like of C# knowledge that is hurting me right now.
If you don't have anything assigned to the datagrid, how does it create the data columns?
If you don't have anything assigned to the datagrid, how does it create the data columns?
I'll have a look at converting to VB.NET for you. As I said, I've done this before but was at my old job so don't have the code to hand.
Basically, this is what happens:
After you've bound the gridview.
Check the amount of rows in gridview
If 0, copy the source of the gridview to a datatable
Adds an empty row to datatable
Rebind the gridview
Hide the empty row
I'll get back to your shortly
Basically, this is what happens:
After you've bound the gridview.
Check the amount of rows in gridview
If 0, copy the source of the gridview to a datatable
Adds an empty row to datatable
Rebind the gridview
Hide the empty row
I'll get back to your shortly
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I eventually did find a way to convert the c# to vb.net, this link is really helpful incase you ever need to pass it onto someone.
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
I got it to work, and I did not have to kick my self as I thought I would, so thanks again for your help on this one.
For the record, was using datagrid with asp.net 1.1 (long story short, just started here recently at my current job and they are still in the land of 1.1, which is funny considering my previous job was the land of 2.0).
Thanks again for your help.
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
I got it to work, and I did not have to kick my self as I thought I would, so thanks again for your help on this one.
For the record, was using datagrid with asp.net 1.1 (long story short, just started here recently at my current job and they are still in the land of 1.1, which is funny considering my previous job was the land of 2.0).
Thanks again for your help.
ASKER
thanks again, got it to work.
found this link to convert c# to vb.net
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
found this link to convert c# to vb.net
http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
Good stuff - I've just converted it so for anyone else that may be interested:
Private Sub FixEmptyGridView(ByVal gv As GridView)
If gv.Rows.Count = 0 And gv.DataSource Is DBNull.Value = False Then
Dim dt As DataTable
If gv.DataSource.GetType.Name = "DataSet" Then
dt = CType(gv.DataSource, DataSet).Tables(0)
ElseIf gv.DataSource.GetType.Name = "DataTable" Then
dt = CType(gv.DataSource, DataTable)
End If
If dt Is Nothing Then
Return
End If
dt.Rows.Add(dt.NewRow)
gv.DataSource = dt
gv.DataBind()
gv.Rows(0).Visible = False
gv.Rows(0).Controls.Clear( )
End If
End Sub
Private Sub FixEmptyGridView(ByVal gv As GridView)
If gv.Rows.Count = 0 And gv.DataSource Is DBNull.Value = False Then
Dim dt As DataTable
If gv.DataSource.GetType.Name
dt = CType(gv.DataSource, DataSet).Tables(0)
ElseIf gv.DataSource.GetType.Name
dt = CType(gv.DataSource, DataTable)
End If
If dt Is Nothing Then
Return
End If
dt.Rows.Add(dt.NewRow)
gv.DataSource = dt
gv.DataBind()
gv.Rows(0).Visible = False
gv.Rows(0).Controls.Clear(
End If
End Sub
I had exactly the same problem and I'll point you to http://www.codeproject.com/useritems/Fix_empty_GridView_issue.asp which should solve your problem.