BindData to Datagrid multiColumn

asp.net Viet Nam
  • 12 years ago

    My example its quite simple :

    2 tables , 1 has categoryCode and categoryName and the other has employeeCode,employeeName and categoryCode

    I need to fill a Datagrid with :

    1. employeeCode - Textbox not Editable
    2. employeeName - Textbox not Editable
    3. categoryName - DropDownList Editable

    I've made the 3rd column a template and inserted a Dropdownlist.
    I can populate all rows with all categories but i cant assign each row its own category.

    Can you help me please??

    Ricardo
     

     

     

     

  • 12 years ago

     Hi, I couldnt make the changes working.
    This is the code im using right now :

    HTML side:
    <asp:DropDownList id=DropDownList2 runat="server" Font-Names="Tahoma" Font-Size="11px" Width="112px" DataSource="<%#BindState()%>"  DataTextField="nameT" DataValueField="code"></asp:DropDownList>

    VB.Net side:

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            If Not IsPostBack Then

                bindData()

            End If
           

        End Sub


        Sub bindData()
            Dim conn As New SqlClient.SqlConnection
            conn.ConnectionString = ConfigurationSettings.AppSettings("SqlConn")
            conn.Open()
           
            Dim ds As New DataSet     
            Dim da As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter("SELECT r.code AS requestCode, e.name AS entName, r.localization, r.efectivedate, r.toinvoice " & _
                " FROM request r INNER JOIN " & _
                " entity e ON r.destinationentity = e.code " & _
                " WHERE (r.state = 1) AND (e.state = 1) and groupid='" & Session("groupId") & "'", conn)

            Session.Remove("groupId")
            da.Fill(ds)

            DataGrid1.DataSource = ds
            DataGrid1.DataBind()
        End Sub
        Public Function BindState()

            Dim funcVb As New funcoesVb
            Dim dtt As New DataTable


            Dim strVal As String = "0| "
            dtt = funcVb.GetDr("select code as code, name as nameT from entitylocalization where state=1", strVal)

            Return dtt



        End Function

     

    What am i doing wrong???

     


     

  • 12 years ago

     

    Thank you for your help i have solved it , but i had to fight code results a bit.
    I dont know if this is the best way to do it ... but it works so its fine by me...

    Cliente Side:
    datasource='<%# BindLocation(DataBinder.Eval(Container, "DataItem.localization")) %>' DataTextField="nameT" DataValueField="code"

    Server Side :

    Public Function BindLocation(ByVal valueLocation As Object)

            Dim funcVb As New funcoesVb
            Dim dtt As New DataTable
            'Gets all Locations
            dtt = funcVb.GetDr("select code as code, name as nameT from entitylocalization where state=1")


            Dim i As Integer
            Dim dtt2 As New DataTable
            dtt2.Columns.Add(funcVb.addColumn("System.String", "code"))
            dtt2.Columns.Add(funcVb.addColumn("System.String", "nameT"))
            Dim dtRw As DataRow
            'Find Location and placed in first place
            For i = 0 To dtt.Rows.Count() - 1
                If dtt.Rows(i).Item(0) = valueLocation Then
                    dtRw = dtt2.NewRow
                    dtRw(0) = dtt.Rows(i).Item(0)
                    dtRw(1) = dtt.Rows(i).Item(1)
                    dtt2.Rows.Add(dtRw)
                    Exit For
                End If
            Next
            'If there's no defaultLocation set Location empty
            If dtt2.Rows.Count = 0 Then
                dtRw = dtt2.NewRow
                dtRw(0) = "0"
                dtRw(1) = " "
                dtt2.Rows.Add(dtRw)
            End If
            'Set other locations next
            For i = 0 To dtt.Rows.Count() - 1
                If dtt.Rows(i).Item(0) <> valueLocation Then
                    dtRw = dtt2.NewRow
                    dtRw(0) = dtt.Rows(i).Item(0)
                    dtRw(1) = dtt.Rows(i).Item(1)
                    dtt2.Rows.Add(dtRw)
                End If
            Next


            Return dtt2

        End Function

     


     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” - Tom Cargill