Subject: Get Data from Datagrid
Posted By: rstelma Post Date: 9/20/2006 5:42:16 PM
Hi All,

I have a datagrid that has one column built from a querystring. The datagrid also has a ddl in it. I want to use data in the datagrid to insert entries into a look up table that will be used to clean data that is being uploaded from Excel files. I saw some code in C# that used the headertext and a for loop to get the data I'm just not sure how to go about it in VB.

Here's the code to get the first column into the datagrid:


Dim CompsNot As Array

CompsNot = Split(_Comps, ", ")
Dim i As Integer
Dim Values As New ArrayList
For i = 0 To UBound(CompsNot)
    Values.Add(Replace(CompsNot(i), ",", ""))
Next
grdCompsNot.DataSource = Values
grdCompsNot.DataBind()


Here is the front page code where I'm also adding the ddl and calling a function to populate the ddl.

<asp:DataGrid ID="grdCompsNot" AutoGenerateColumns="False" Runat="server" class="datagrid" >
<Columns>
<asp:templatecolumn headertext="Bad Name">
<itemtemplate>
<%# Container.DataItem %>
</itemtemplate>
</asp:templatecolumn>
<asp:TemplateColumn HeaderText="Good Name">
<ItemTemplate>
<asp:DropDownList ID="ddlCompID" Runat="server"  DataTextField = "GoodName" DataValueField = "CompID" DataSource='<%# getCompID() %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>


Any help would be greatly appreciated.

Thanks.

Richard

Reply By: rstelma Reply Date: 9/20/2006 7:04:02 PM
OK.

Since I already knew how to get data from a repeater, I changed the datagrid to a repeater and can now use the code below to access the data in the repeater.

    Protected Sub rptComponentAliases_ItemDataBound(ByVal s As Object, ByVal e As RepeaterItemEventArgs) Handles rptComponentAliases.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim litCompAliasName As Literal = e.Item.FindControl("litCompAliasName")
            Dim ddlCompID As DropDownList = e.Item.FindControl("ddlCompID")
            Dim SQL As String = "SELECT CompID, CompName FROM Components WHERE Status = 1 ORDER BY CompName;"
            Dim oDA As New SqlDataAdapter(SQL, _oConn)
            Dim oDS As New DataSet
            oDA.Fill(oDS)
            litCompAliasName.Text = e.Item.DataItem
            ddlCompID.DataSource = oDS
            ddlCompID.DataTextField = "CompName"
            ddlCompID.DataValueField = "CompID"
            ddlCompID.DataBind()
            ddlCompID.Items.Insert(0, New ListItem("", 0))
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal s As Object, ByVal e As EventArgs) Handles btnSave.Click
        Try
            _oConn.Open()
            For Each i As RepeaterItem In rptComponentAliases.Items
                Dim litCompAliasName As Literal = i.FindControl("litCompAliasName")
                Dim ddlCompID As DropDownList = i.FindControl("ddlCompID")
                If ddlCompID.SelectedValue > 0 Then
                    Dim SQL As String = "INSERT INTO ComponentAliases (CompID, CompAliasName, Status) VALUES (" & ddlCompID.SelectedValue & ", '" & litCompAliasName.Text & "', 1);"
                    Dim CMD As New SqlCommand(SQL, _oConn)
                    CMD.ExecuteNonQuery()
                End If
            Next
        Catch ex As Exception
        Finally
            _oConn.Close()
        End Try
    End Sub


Works like a champ.

Any comments are greatly appreciated.

Thanks,
Richard


Reply By: carywhittier Reply Date: 9/25/2006 11:28:02 PM
hey richard, im curious are you from Jacksonville FL and did you grow up near Phillips Highway?
peace
carywhittier.com





cwhittier

Go to topic 50093

Return to index page 165
Return to index page 164
Return to index page 163
Return to index page 162
Return to index page 161
Return to index page 160
Return to index page 159
Return to index page 158
Return to index page 157
Return to index page 156