Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a column and row in a grid

Status
Not open for further replies.

DESMAN2003

Programmer
Oct 27, 2003
24
AU
Hi,

I was recently given a program to modify in VB .NET
I am very new to this language and am having trouble trying to select a column and copy it. What is currently there is 3 columns. If you right mouse click on the row, and select "copy", you can copy the whole row. What I am trying to do is make it so that you can right mouse click and choose to either copy column1 or column 2 or column 3 or all of them for the row you are clicking on. I know how to make the menu using the context menu, but what I have no idea about is how to make it copy parts of the row so that it can be "pasted" into notepad or something like that.

Does anyone have any suggestions on how I could do this?? Thanks in advance!
 
Hi Desman
1) Make a context menu and add "Copy","Paste" or whatever u want in that menu ,BIND that Menu to the DATAGRID
2)Put The following Code code in Mouse_Down event,it will tell you if a cell is selected then it will give u the data of that cell and if row then you will get Row
3) Now in the ContextMenu "Copy" you can place the data you get in step 2 using
Clipboard.SetDataObject(TextBox1.Text) 'i m just telling you how u can get data from TextBox1 you can place the data you get in Step 2) in a variable and then set data with this method
4) you can later retrieve the data as well by
by
Dim data As IDataObject = Clipboard.GetDataObject()
' If the data is text, then set the text of the
' TextBox to the text in the Clipboard.
If (data.GetDataPresent(DataFormats.Text)) Then
TextBox1.Text = data.GetData(DataFormats.Text).ToString()
End If

Hope it will help you out
Regards
Nouman


Code for step 2)
Dim pt = New Point(X, Y)

Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt)

If hti.Type = DataGrid.HitTestType.Cell Then

MessageBox.Show(dataGrid1(hti.Row, hti.Column).ToString())

Else

If hti.Type = DataGrid.HitTestType.ColumnHeader Then 'assumes datasource is a dataview

MessageBox.Show(CType(DataGrid1.DataSource, DataView).Table.Columns(hti.Column).ToString())

End If

End If



Nouman Zaheer
Software Engineer
MSR
 
Hi Nouman,

Thanks for your help but the only problem now is that I have been given a new version of the software which has it all represented as a list view. The copy and select options are written as classes and I have created a seperate class called cmnucopydesc. I dont know how to create another list view to reference this class because what is happening is that there are a lot of reports that are referencing the current class. What I need to do for this report is make is generate another list view and reference this new class. I have written the class as follows but it doesnt work properly:


Public Class CMnuCopySiteDesc
Inherits ContextMenu

Protected WithEvents cmnuItemCopy As System.Windows.Forms.MenuItem
Protected WithEvents cmnuItemCopyAll As System.Windows.Forms.MenuItem
Protected WithEvents cmnuItemCopySite As System.Windows.Forms.MenuItem
Protected WithEvents cmnuItemCopyDesc As System.Windows.Forms.MenuItem

Public WithEvents rtf As System.Windows.Forms.ListView

'Provide default menu item for listview control, ie. copy & copy all
Public Sub New()
Me.cmnuItemCopy = New System.Windows.Forms.MenuItem
Me.cmnuItemCopyAll = New System.Windows.Forms.MenuItem
Me.cmnuItemCopySite = New System.Windows.Forms.MenuItem
Me.cmnuItemCopyDesc = New System.Windows.Forms.MenuItem

Me.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.cmnuItemCopy, Me.cmnuItemCopyAll, Me.cmnuItemCopySite, Me.cmnuItemCopyDesc})
'
'cmnuItemCopy
'
Me.cmnuItemCopy.Index = 0
Me.cmnuItemCopy.Text = "Copy Row"
'
'cmnuItemCopyAll
'
Me.cmnuItemCopyAll.Index = 1
Me.cmnuItemCopyAll.Text = "Copy All"
'
'
'cmnuItemCopy for Site No.
'
Me.cmnuItemCopySite.Index = 2
Me.cmnuItemCopySite.Text = "Copy Site Number"
'
'cmnuItemCopy for Description
'
Me.cmnuItemCopyDesc.Index = 3
Me.cmnuItemCopyDesc.Text = "Copy Description"
End Sub

Private Sub cmnuItemCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmnuItemCopy.Click
Dim t, temp As String
Dim i, j, totalItems As Integer

totalItems = rtf.SelectedItems.Count

With rtf
For i = 0 To totalItems - 1
temp = ""
For j = 0 To .Columns.Count - 1
temp = temp & .Items(.SelectedIndices(i)).SubItems(j).Text & ControlChars.Tab
Next
t = t & temp & vbCrLf
Next
Clipboard.SetDataObject(t)
End With
End Sub

Private Sub cmnuItemCopyAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmnuItemCopyAll.Click
Dim t, temp As String
Dim i, j, totalItems As Integer

totalItems = rtf.Items.Count

With rtf
For i = 0 To totalItems - 1
temp = ""
For j = 0 To .Columns.Count - 1
temp = temp & .Items(i).SubItems(j).Text & ControlChars.Tab
Next
t = t & temp & vbCrLf
Next
Clipboard.SetDataObject(t)
End With
End Sub

'Setting up menu items' availability
Protected Overrides Sub OnPopup(ByVal e As System.EventArgs)
If rtf.SelectedIndices.Count = 0 Then
Me.cmnuItemCopy.Enabled = False
Me.cmnuItemCopyAll.Enabled = True
ElseIf Len(rtf.Items(rtf.SelectedIndices(0)).Text) < 1 Then
Dim i As Integer
For i = 0 To Me.MenuItems.Count - 1
Me.MenuItems(i).Enabled = False
Next
' ElseIf len(rtf.SelectedText.Length < 1 Then
' Me.cmnuItemCopy.Enabled = False
' Me.cmnuItemSelectAll.Enabled = True
Else
Me.cmnuItemCopy.Enabled = True
Me.cmnuItemCopyAll.Enabled = True
End If
End Sub

Private Sub cmnuItemCopySites_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmnuItemCopySite.Click
Dim t, temp As String
Dim i, j, totalItems As Integer

totalItems = rtf.SelectedItems.Count

With rtf
For i = 0 To totalItems - 1
temp = &quot;&quot;
For j = 0 To .Columns.Count - i
temp = temp & .Items(.SelectedIndices(i)).Text & ControlChars.Tab
Next
t = t & temp & vbCrLf
Next
Clipboard.SetDataObject(t)
End With
End Sub

End Class

The function that references this is written as:


Public Sub mnuDisplaySiteDescriptions_Popup(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mnuDisplaySiteDescriptions.Popup
mnuDisplaySiteDescriptions_Click(eventSender, eventArgs)
End Sub
Public Sub mnuDisplaySiteDescriptions_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mnuDisplaySiteDescriptions.Click
'
' Report shows site number and description for user-entered site numbers.
'
Dim SiteDescriptions As New ADODB.Recordset
Dim Report As String
Dim i, j, RowsCount As Integer
Dim colwidth As Object
'
'***old***
'frmSiteDescriptions.DefInstance.ShowDialog()

'***new***
Dim frmSiteDescriptionsInst As frmSiteDescriptions = New frmSiteDescriptions
frmSiteDescriptionsInst.ShowDialog()
frmSiteDescriptionsInst.Dispose()

If blnCancel Then Exit Sub
'
SiteDesc = &quot;Select Site_no as &quot;&quot;Site no&quot;&quot;, RName||' Map ref: '||Directory||':'||Directory_Ref as &quot;&quot;Description and map ref&quot;&quot; from Sites &quot; & &quot;where Site_no in (&quot; & SiteDesc & &quot;) order by Site_no;&quot;
'MsgBox SiteDesc, vbOKOnly, &quot;Code&quot;
SiteDescriptions = Oracle.Execute(SiteDesc, , ADODB.CommandTypeEnum.adCmdText)
If SiteDescriptions.RecordCount > 0 Then
With grdDisplay1
RowsCount = SiteDescriptions.RecordCount + 2 'report heading + field name

.BeginUpdate()

.Columns.Clear()
.Items.Clear()

.Columns.Add(&quot;&quot;, 70, HorizontalAlignment.Left)
.Columns.Add(&quot;&quot;, 400, HorizontalAlignment.Left)

rptName1.Width = 470
rptName1.Text = &quot; &quot;

Dim lvl1 As New ListViewItem
lvl1.Font = New System.Drawing.Font(&quot;Microsoft Sans Serif&quot;, 8.25!, System.Drawing.FontStyle.Bold)
lvl1.Text = &quot;Site no&quot;
lvl1.SubItems.Add(&quot;Description and Map Ref&quot;)
.Items.Add(lvl1)
'
' Populate display
'
If Not SiteDescriptions.BOF Then SiteDescriptions.MoveFirst()
'
For j = 2 To RowsCount - 1
System.Windows.Forms.Application.DoEvents()
Dim itm As ListViewItem = New ListViewItem

If IsDBNull(SiteDescriptions.Fields(0).Value) Then
itm.Text = &quot; &quot;
Else
itm.Text = SiteDescriptions.Fields(0).Value
End If

For i = 1 To SiteDescriptions.Fields.Count - 1
If IsDBNull(SiteDescriptions.Fields(i).Value) Then
itm.SubItems.Add(&quot; &quot;)
Else
itm.SubItems.Add(SiteDescriptions.Fields(i).Value)
End If
Next
.Items.Add(itm)
SiteDescriptions.MoveNext()
Next
'
' Show report
'
Me.Width = 490
Me.Height = 280
' .Width = 475

rtfReport.Visible = False

.Visible = True

.EndUpdate()

End With
'
mnuFileExportToExcel.Enabled = True
DisplayType = GridDisplay
PrintDoc.DefaultPageSettings.Landscape = True
mnuFileExportToExcel.Enabled = True
DisplayType = GridDisplay
Else
DisplayType = TextDisplay
rtfReport.Text = &quot;Site descriptions not available.&quot;
rtfReport.Visible = True
grdDisplay1.Visible = False
rptName1.Visible = False
End If
 
Hi Desmon
which part of the code is not working for you?
who wrote this class? if its written by you then you should have fearly good idea whats is this and whats going wrong
but if you can explain what you want then it can be explained in better way
Regards
Nouman


Nouman Zaheer
Software Engineer
MSR
 
thanks Nouman for helping.... I expanded the class to include using a copy menu item to select the first row only... all I had to do is slightly modify it for its j and i coordinates but positioning the j to 0, then to 1, depending on the column number. It was actually very easy, I think that when I am sick, I cant think properly :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top