Question Putting the selected item into the correct columns

firehuman

Member
Joined
Jun 4, 2013
Messages
8
Programming Experience
1-3
Hi there,

I have an combo box (Not the datagridviewcombobox) which has all the xml.file data label like this: Untitled.png

And this is my xml.file:
VB.NET:
<?xml version="1.0" standalone="yes"?>
<programsetting>
  <panel>
    <fontstyle>A</fontstyle>
    <fontsize>13</fontsize>
    <location>100,100</location>
  </panel>
  <panel>
    <fontstyle>B</fontstyle>
    <fontsize>12</fontsize>
    <location>100,100</location>
  </panel>
  <test>
</programsetting>

Then I also do have a datagridview which is empty. But as you can see the Untitled.png
The button 1 it contain of:
VB.NET:
Dim col As New DataGridViewTextBoxColumn()        Dim colsize As New DataGridViewTextBoxColumn()
        Dim colloc As New DataGridViewTextBoxColumn()
        Dim dgvRow As New DataGridViewRow
        MainForm.dgv.Rows.Clear()
        MainForm.dgv.Columns.Clear()


        col.Name = ("fontstyle")
        MainForm.dgv.Columns.Add(col)


        colsize.Name = ("fontsize")
        MainForm.dgv.Columns.Add(colsize)


        colloc.Name = ("location")
        MainForm.dgv.Columns.Add(colloc)


        MainForm.dgv.Rows.Add(Me.ComboBox1.SelectedItem())

So my question now is that, how to I actually add the correct selected item from combobox into the datagridview which will then put 'A' into the fontstyle column, '12' into fontsize column and '100,100' into "location"?

THANK YOU
 
Last edited by a moderator:
You can add items to the ComboBox that display the combined String but actually have distinct properties for each of the three values. When the user makes a selection, you get the SelectedItem and get those three property values from it. You can then do as you like with them, including add a new row to a DataGridView with them as field values. The class you use for the items might look like this:
Imports System.Drawing

Public Class Class1

    Public Property FontStyle() As FontStyle
    Public Property FontSize() As Single
    Public Property Location() As Point

    Public Overrides Function ToString() As String
        Return String.Format("{0}, {1}, {2}", FontStyle, FontSize, Location)
    End Function

End Class
Adding instances of that class to your ComboBox will display the result the ToString method but you can cast the SelectedItem as Class1 (or whatever you name it) and then access the three separate properties. You can then call Rows.Add on your grid and simply pass the three values.
 
The main thing now as you can see inside of xml coding, i want them to display data into each of the columns. Like I do have 3 columns which are 'fontstyle', 'fontsize' and 'location'. So after selecting item from combobox, like example the picture: A13100,100 as you can see A is the fontstyle, 13 is the fontsize and 100,100 is the location. So after selecting the items, i want to add them into the datagridview by clicking on the 'Button 1' and it will display according.
 
Thanks for the reply.

Forgotten to mention that I do have 2 Forms. But for now the picture in post #1, it's form 2. Which contain Combobox and Button. So what I did was in Form 1, I select a collection in a combobox which is 'Panel" and then click on a button to Load a pop out (Which is Form2) then it will contain whatever data inside of "panel" from the xml.file so it has A & B so when I select one of them like 'A12100,100' So then i click on button1 to display it back to Form 1 datagridview which the coding is
VB.NET:
Dim col As New DataGridViewTextBoxColumn()        Dim colsize As New DataGridViewTextBoxColumn()
        Dim colloc As New DataGridViewTextBoxColumn()
        Dim dgvRow As New DataGridViewRow
        MainForm.dgv.Rows.Clear()
        MainForm.dgv.Columns.Clear()


        col.Name = ("fontstyle")
        MainForm.dgv.Columns.Add(col)


        colsize.Name = ("fontsize")
        MainForm.dgv.Columns.Add(colsize)


        colloc.Name = ("location")
        MainForm.dgv.Columns.Add(colloc)


        MainForm.dgv.Rows.Add(Me.ComboBox1.SelectedItem())

But then the result will then be at fontstyle only.
Untitled2.png

So what I want is to be like xml.file details into the correct columns in datagridview.

Sorry if it's abit confusing.
 
Last edited by a moderator:
Firstly, you might want to stop posting so much blank space. It makes your posts harder to read and anything that does that reduces your chances of getting help. As for what you said in post #5, it makes no difference. You still need to do exactly the same thing and the only difference is that you need to pass the object selected from the second form back to the first, which you would do via a property in the second form that the first from can get.
 
Firstly, you might want to stop posting so much blank space. It makes your posts harder to read and anything that does that reduces your chances of getting help. As for what you said in post #5, it makes no difference. You still need to do exactly the same thing and the only difference is that you need to pass the object selected from the second form back to the first, which you would do via a property in the second form that the first from can get.

Sorry for posting those blanks. Yeah i get it now from your 2nd post. THANK YOU!!! CHEERS!
 
You can add items to the ComboBox that display the combined String but actually have distinct properties for each of the three values. When the user makes a selection, you get the SelectedItem and get those three property values from it. You can then do as you like with them, including add a new row to a DataGridView with them as field values. The class you use for the items might look like this:
Imports System.Drawing

Public Class Class1

    Public Property FontStyle() As FontStyle
    Public Property FontSize() As Single
    Public Property Location() As Point

    Public Overrides Function ToString() As String
        Return String.Format("{0}, {1}, {2}", FontStyle, FontSize, Location)
    End Function

End Class
Adding instances of that class to your ComboBox will display the result the ToString method but you can cast the SelectedItem as Class1 (or whatever you name it) and then access the three separate properties. You can then call Rows.Add on your grid and simply pass the three values.

So after adding in the code, I've change "MainForm.dgv.Rows.Add(Me.ComboBox1.SelectedItem(FontStyle))"
VB.NET:
[COLOR=#333333][FONT=Consolas]Public Property FontStyle() As FontStyle[/FONT][/COLOR][COLOR=#333333][FONT=Consolas]    
    Public Property FontSize() As Single[/FONT][/COLOR]
[COLOR=#333333][FONT=Consolas]    Public Property Location() As Point
[/FONT][/COLOR]


So I was able to get the FontSize only. Like either A or B will be input into the datagridview. But can u explain more on the
VB.NET:
[COLOR=#333333][FONT=Consolas]
Public Overrides Function ToString() As String[/FONT][/COLOR]
[COLOR=#333333][FONT=Consolas]        Return String.Format("{0}, {1}, {2}", FontStyle, FontSize, Location)[/FONT][/COLOR]
[COLOR=#333333][FONT=Consolas]    EndFunction[/FONT][/COLOR]

Now I'm having trouble putting the other value in FontSize and Location.

 
You're not doing what I said. Read what I posted and follow the instructions.

1. cast the SelectedItem as Class1 (or whatever you name it)
2. access the three separate properties
3. call Rows.Add on your grid and simply pass the three values
 
You're not doing what I said. Read what I posted and follow the instructions.

1. cast the SelectedItem as Class1 (or whatever you name it)
2. access the three separate properties
3. call Rows.Add on your grid and simply pass the three values

Sorry, i just started learning programming language.
Can you provide an example on how to cast and access the properties?
I dont really understand by cast the SelectedItem as Class1 and accessing
 

Similar threads

Back
Top