Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to use Data-Grid(VB.Net)

ksmuchksmuch Member Posts: 3
I can't use data-grid to specify Size of column or move text to the cell of data-grid please help me (Using VB.Net)

Comments

  • bednarjmbednarjm Member Posts: 82
    : I can't use data-grid to specify Size of column or move text to the cell of data-grid please help me (Using VB.Net)
    :
    :

    For the col width look at setting up an asp template.

    As to moving text you can either:

    1) Directly bind a recordset to the datagrid
    [code]
    dtGrid.DataSource = RecordSet ' this can be any recordset
    ' you have created
    dtGrid.DataBind()

    [/code]

    2) Create a data table, use this to go through your recordset (one
    row at a time) and bind this to the datagrid:

    [code]
    Dim dt As New DataTable("Emps")
    With dt.Columns
    .Add("Course Name")
    .Add("Method")
    .Add("Session Id")
    .Add("Date Attended")
    End With
    dt.MinimumCapacity = 50
    dt.CaseSensitive = False

    Do While dr.RS.Read()
    Dim dtrow = dt.NewRow()
    dtrow("Course Name") = dr.RS.Item("Course Name")
    dtrow("Method") = dr.RS.Item("Method")
    dtrow("Session Id") = dr.RS.Item("Session Id")
    dtrow("Date Attended") = dr.RS.Item("Date Attended")
    dt.Rows.Add(dtrow)
    Loop
    drResults.DataSource = dt
    drResults.DataBind()
    [/code]

    Play with this stuff - it's very powerful.

    jim b
  • robban303robban303 Member Posts: 6
    Hi

    I am used to working with Recordsets in VB6, to for example put customers from a Access-Database-Table into a List or Combobox. How can I do this in similar way with .Net?

    Robert

  • bednarjmbednarjm Member Posts: 82
    Let's see (using ASP.NET):

    To bind to a combobox:

    [code]
    Private Sub LoadEmployees()
    If Not Page.IsPostBack Then
    ' I've create an object to hold common
    ' database activities
    Dim lr As New cProcs()
    Dim cmdTxt = yoursql
    With lr
    .sql = cmdTxt
    .ExecReader()
    End With
    ' this will take the recordsed generated and
    ' bind to a listbox (asp.net control)
    With lstEmps
    .DataSource = lr.RS
    .DataTextField = "e1"
    .DataBind()
    End With
    lr = Nothing
    lblTitle.Visible = False
    End If
    End Sub
    [/code]

    To use a datagrid (with some some simple formatting):

    [code]

    Dim dr As New cProcs()

    dr.sql = sql
    dr.ExecReader()

    ' use the data table to hold info in memory
    ' see any ADO.NET book for more info on this

    Dim dt As New DataTable("Emps")
    With dt.Columns
    .Add("Course Name")
    .Add("Method")
    .Add("Session Id")
    .Add("Date Attended")
    End With
    dt.MinimumCapacity = 50
    dt.CaseSensitive = False

    Dim sCourse As String
    Dim sMethod As String
    Dim iId As Integer
    Dim sDte As String

    ' this walks through the recordset generated record by
    ' record using the datatable to hold the data.
    ' We could have directly bound the recordset to the
    ' datagrid - this example show's the row-by-row
    ' method - other uses could be conditional formatting
    ' (break on name, etc.)

    Do While dr.RS.Read()
    Dim dtrow = dt.NewRow()
    sCourse = dr.RS.Item("Course Name")
    sMethod = dr.RS.Item("Method")
    iId = dr.RS.Item("Session Id")
    sDte = dr.RS.Item("Date Attended")
    ' example control break
    If sCourse <> "TEST" then
    dtrow("Course Name") = sCourse
    else
    dtrow("Course Name") = ""
    end if
    dtrow("Method") = sMethod
    dtrow("Session Id") = iId
    dtrow("Date Attended") = sDte
    dt.Rows.Add(dtrow)
    Loop
    drResults.DataSource = dt
    drResults.DataBind()

    dr = Nothing

    [/code]


    jim b
  • Andre YoungAndre Young USAMember Posts: 0

    _____ < http://forcoder.org /> free video tutorials and ebooks about // Python Scratch R Java MATLAB Assembly PL/SQL Swift Perl C PHP Ruby Visual Basic .NET Objective-C C++ Go Delphi C# Visual Basic JavaScript Bash Alice Prolog SAS Rust Crystal Lisp Clojure Dart Ada ML Lua Logo Awk F# Erlang Apex COBOL LabVIEW ABAP Fortran VBScript Transact-SQL Hack Scheme FoxPro Kotlin D Julia Scala // ______________

Sign In or Register to comment.