Results 1 to 5 of 5

Thread: [RESOLVED] How to programmatically add columns with data to listview or datagridview?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Resolved [RESOLVED] How to programmatically add columns with data to listview or datagridview?

    Hi!

    I have a small issue I have been working on.

    A UI has a search button and a couple of check boxes. Depending on what check boxes are checked, certain columns of data sould be listed in the "grid".

    SInce even the column title is dynamically based on the data (show sums and stuff) I would prefer to do it like this:

    create column
    set title
    set all cells/rows
    add to grid

    I have been messing with the listview a few hours now and can't find a way to do this in a good way, with a minimum amount of code. All my attempts have been unsuccessful. I would love to do this:

    listview1.Columns(calculatedColumnText).rows.addrange(result.Select(x => x.dataforcol1).ToArray().

    But the above statement exist only in my dreams...

    Anyone, please help!

    /Henrik

  2. #2
    Lively Member hb21l6's Avatar
    Join Date
    Sep 2008
    Location
    UK - East Yorkshire
    Posts
    126

    Re: How to programmatically add columns with data to listview or datagridview?

    not sure about listviews, but in Datagridviews its easy..

    In my example, I have an array called CollectDataArray(12,45) which I loop through,.
    12 rows, 45 columns.


    ' first we define the head for each column.
    DataGridView1.ColumnCount = 45
    For C As Integer = 0 To 44
    DataGridView1.Columns(C).Name = "Col" & C
    Next

    ' now we write the data to the cells/ rows.

    For n As Integer = 0 To UBound(CollectDataArray, 2) - 1
    Dim dgvRow As New DataGridViewRow
    Dim dgvCell As DataGridViewCell
    For p As Integer = 0 To 44
    dgvCell = New DataGridViewTextBoxCell()
    dgvCell.Value = CollectDataArray(p, n)
    dgvRow.Cells.Add(dgvCell)
    Next
    DataGridView1.Rows.Add(dgvRow)
    Next
    DataGridView1.Refresh()


    I'm sure you can take this apart to build a few columns. I just keep this code to dynamically build DGVs.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to programmatically add columns with data to listview or datagridview?

    How are you storing the cols on/off flag?

    Here's how I'd do it?
    turn off grid updating (SuspendLayout)
    create data
    Bind data to grid
    Hide cols I don't want
    turn on grid updating (ResumeLayout)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: How to programmatically add columns with data to listview or datagridview?

    Quote Originally Posted by hb21l6 View Post
    not sure about listviews, but in Datagridviews its easy..

    In my example, I have an array called CollectDataArray(12,45) which I loop through,.
    12 rows, 45 columns.


    ' first we define the head for each column.
    DataGridView1.ColumnCount = 45
    For C As Integer = 0 To 44
    DataGridView1.Columns(C).Name = "Col" & C
    Next

    ' now we write the data to the cells/ rows.

    For n As Integer = 0 To UBound(CollectDataArray, 2) - 1
    Dim dgvRow As New DataGridViewRow
    Dim dgvCell As DataGridViewCell
    For p As Integer = 0 To 44
    dgvCell = New DataGridViewTextBoxCell()
    dgvCell.Value = CollectDataArray(p, n)
    dgvRow.Cells.Add(dgvCell)
    Next
    DataGridView1.Rows.Add(dgvRow)
    Next
    DataGridView1.Refresh()


    I'm sure you can take this apart to build a few columns. I just keep this code to dynamically build DGVs.
    Interesting thought, but not exactly what I need.

    I have 5 checkboxes representing 5 columns in the grid with statistical data. 3 columns in the grid are mandatory and should always be visible. So that is 8 columns total if everything is selected.

    For this scenario, I would just love to build my grid "by column" instead of "by row". If I do it "by row" I have to keep track of which columns are chose, and what their position/name is. I don't like hard coded indexers or magic strings.

    It would be a two way solution, with:

    1) Add the columns to the grid, with an if(chkColumnX.checked) for each of the optional columns.

    2) Second when I start adding the row data, I need to know which columns the user actually selected. I don't know which of the optional ones the user selected, therefor I have no way of knowing by index which columns I have in the grid. SOmething like

    var item = new DataListItem;
    item.text = "Col0Value";

    item.Subitems.Add("col1Value");
    item.Subitems.Add("Col2Value");

    ... then which of the optional columns should go here???? It could be any one of the 5 optional ones, if I only checked one of them. How do I link the datasource item with the columns in the grid? I need to know which of the properties in the datasource row I should put here as the next subitem. I reckon it would be equally problematical with a datagridview.

    I hope this gices a better explanation of my dilemma. I really want to avoid lots of if-then cases for each of the 5 optional textboxes. I would prefer to have them once, and do everything "by column".

    Any other ideas?

    /Henrik

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to programmatically add columns with data to listview or datagridview?

    as I said... load the data and then simply hide and show columns...

    Code:
        Private Sub LoadAndShowData()
    
            If DataGridView1.DataSource Is Nothing Then
                Dim dt As New DataTable
                dt.Columns.Add("Col1")
                dt.Columns.Add("Col2")
                dt.Columns.Add("Col3")
                dt.Columns.Add("Col4")
                dt.Columns.Add("Col5")
                dt.Columns.Add("Col6")
                dt.Columns.Add("Col7")
                dt.Columns.Add("Col8")
    
                For a = 1 To 100
                    Dim dr As DataRow = dt.NewRow
                    dr("Col1") = String.Format("Col {0} Row {1}", 1, a)
                    dr("Col2") = String.Format("Col {0} Row {1}", 2, a)
                    dr("Col3") = String.Format("Col {0} Row {1}", 3, a)
                    dr("Col4") = String.Format("Col {0} Row {1}", 4, a)
                    dr("Col5") = String.Format("Col {0} Row {1}", 5, a)
                    dr("Col6") = String.Format("Col {0} Row {1}", 6, a)
                    dr("Col7") = String.Format("Col {0} Row {1}", 7, a)
                    dr("Col8") = String.Format("Col {0} Row {1}", 8, a)
    
                    dt.Rows.Add(dr)
    
                Next
    
                DataGridView1.DataSource = dt
    
    
            End If
    
            DataGridView1.Columns("Col4").Visible = CheckBox1.Checked
            DataGridView1.Columns("Col5").Visible = CheckBox2.Checked
            DataGridView1.Columns("Col6").Visible = CheckBox3.Checked
            DataGridView1.Columns("Col7").Visible = CheckBox4.Checked
            DataGridView1.Columns("Col8").Visible = CheckBox5.Checked
    
    
        End Sub
    One form, an empty DGV and 5 checkboxes...
    In the form load I call the above sub to load and set the cols...

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            LoadAndShowData()
        End Sub
    Then in the CheckedChange event of the checkboxes, I set the col visibility accordingly:
    Code:
        Private Sub ColOptionsCheckedChanged(sender As Object, e As EventArgs) Handles CheckBox5.CheckedChanged, CheckBox4.CheckedChanged, CheckBox3.CheckedChanged, CheckBox2.CheckedChanged, CheckBox1.CheckedChanged
            LoadAndShowData()
        End Sub
    I check the Datasource of the grid so I only load the data once... after that, I'm simply hiding columns based on the corresponding checkbox. Initially the checkboxes are unchecked, so all that shows are cols 1-3 ... the 5 checkboxes represent cols 4-8 ...

    Nice and easy...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width