Logo 
Search:

Asp.net Answers

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds
  Question Asked By: Jason Perry   on Oct 31 In Asp.net Category.

  
Question Answered By: Jackson Williams   on Oct 31

I think I understand what "devicekid" means by storing temporary data. I to have
been looking for a way to do that.
Most of the time we just use a datagrid  bound to a database or a xml file.
In my case, I'm using a datagrid that starts empty and the user has to insert
data. I store the data in an Arraylist.

Now, how would I read all elements from the arraylist and send them to the
database?
Thank you for your attention.


Here's my code for the database:


<code>
<codebehind>
Private Sub LoadGrid()

If (Session("Dados") Is Nothing) Then

Dim dt As DataTable

Dim dr As DataRow

Dim i As Integer

dt = New DataTable()

dt.Columns.Add(New DataColumn("Linha", GetType(String)))

dt.Columns.Add(New DataColumn("Ref", GetType(String)))

dt.Columns.Add(New DataColumn("Desc", GetType(String)))

dt.Columns.Add(New DataColumn("Unidade Medida", GetType(String)))

dt.Columns.Add(New DataColumn("Quantidade", GetType(String)))

'For i = 1 To 2

' dr = dt.NewRow()

' dr(0) = "Item " + i.ToString()

' dr(1) = "Item " + i.ToString()

' dr(2) = "Item " + i.ToString()

' dr(3) = "Item " + i.ToString()

' dr(4) = "Item " + i.ToString()

' dt.Rows.Add(dr)

'Next

Session("Dados") = dt

End If

'

With Me.dgMrLines

.DataSource = New DataView(Session("Dados"))

.DataBind()

End With

End Sub

'

'

Public Sub dgMrLines_ItemCommand(ByVal source As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)

If e.CommandName = "Insert" Then

Dim dr As DataRow

dr = DirectCast(Session("Dados"), DataTable).NewRow()

dr(0) = DirectCast(e.Item.FindControl("txtLinha"), TextBox).Text

dr(1) = DirectCast(e.Item.FindControl("txtRef"), TextBox).Text

dr(2) = DirectCast(e.Item.FindControl("txtDesc"), TextBox).Text

dr(3) = DirectCast(e.Item.FindControl("txtUnit"), TextBox).Text

dr(4) = DirectCast(e.Item.FindControl("txtQty"), TextBox).Text

DirectCast(Session("Dados"), DataTable).Rows.Add(dr)

Call Me.LoadGrid()

End If

End Sub

'

'

Public Sub dgMrLines_DeleteCommand(ByVal source As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs)

DirectCast(Session("Dados"), DataTable).Rows.RemoveAt(e.Item.ItemIndex)

Call Me.LoadGrid()

End Sub

</codebehind>


<aspx code>

<asp:DataGrid id="dgMrLines" OnItemCommand="dgMrLines_ItemCommand"
OnDeleteCommand="dgMrLines_DeleteCommand" style="Z-INDEX: 101; LEFT: 22px;
POSITION: absolute; TOP: 19px" runat="server" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4"
GridLines="Vertical" ForeColor="Black" Font-Names="Arial" Font-Size="X-Small"
AutoGenerateColumns="False" ShowFooter="True">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#CE5D5A"></SelectedItemStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#F7F7DE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#6B696B"></HeaderStyle>
<FooterStyle BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="Linha">
<ItemTemplate>
<asp:Label id="lblLinha" runat="server">
<%# DataBinder.Eval(Container.DataItem, "Linha")%>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtLinha" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Ref">
<ItemTemplate>
<asp:Label id="lblRef" runat="server">
<%# DataBinder.Eval(Container.DataItem, "Ref")%>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtRef" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Desc">
<ItemTemplate>
<asp:Label id="lblDesc" runat="server">
<%# DataBinder.Eval(Container.DataItem, "Desc")%>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtDesc" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Unidade Medida">
<ItemTemplate>
<asp:Label id="lblUnit" runat="server">
<%# DataBinder.Eval(Container.DataItem, "Unidade Medida")%>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtUnit" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Quantidade">
<ItemTemplate>
<asp:Label id="lblQty" runat="server">
<%# DataBinder.Eval(Container.DataItem, "Quantidade")%>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox id="txtQty" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button id="btnDelete" runat="server" Width="20px"
CommandName="Delete" Text="X"></asp:Button>
</ItemTemplate>
<FooterTemplate>
<asp:Button id="btnInsert" runat="server" Width="20px"
CommandName="Insert" Text="+"></asp:Button>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#F7F7DE"
Mode="NumericPages"></PagerStyle>
</asp:DataGrid>

</aspx code>

</code>

Share: 

 

This Question has 10 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on datagrid Or get search suggestion and latest updates.


Tagged: