janue

hi, I want the captiontext of my datagrid to display the data of the cell I selected, I tried alot of ways and search throughout the Internet but no avail. My project was passed down and the older version is able to display the selected cell's data into the captiontext of the datagrid, but its done by configuring the properties of the datagrid in design view instead of coding. Can this be done by coding I am truly grateful for your help

P.S.: Its my first time here, so if I had posted the topic onto the wrong forum, please forgive me! >.<




Re: Windows Forms General need help with Datagrid's Captiontext

Merin Gazell

Use the HeaderText property of the column accessible by the Columns collection.



Re: Windows Forms General need help with Datagrid's Captiontext

janue

I cant find the Columns collection, where issit I use dataset btw....




Re: Windows Forms General need help with Datagrid's Captiontext

Merin Gazell

Sorry, I thought you were talking about Datagridview. Check the below link.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridcolumnstyle.headertext(vs.71).aspx





Re: Windows Forms General need help with Datagrid's Captiontext

janue

im very sry, but I have no idea how to put those codes into my project. Here are my codes, I dunno where to type it in:

Private Sub TimeAtt_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

DataGrid1.ParentRowsVisible = False

' create a connection string

Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database.mdb"

Dim myConnection As OleDbConnection = New OleDbConnection

myConnection.ConnectionString = connString

' create a data adapter

Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT username, empno FROM emp", myConnection)

' create a new dataset

Dim ds As DataSet = New DataSet

' fill dataset

da.Fill(ds, "emp")

DataGrid1.SetDataBinding(ds, "emp")

' Attach DataSet to DataGrid

DataGrid1.DataSource = ds.DefaultViewManager

End Sub






Re: Windows Forms General need help with Datagrid's Captiontext

mberseth

Hello janue -

I am not a DataGrid expert, but maybe the following code snippet can get you going in the right direction ...

I am subscribing to the CurrentCellChanged event on the DataGrid and using the row/column index to get the data from the cell and set the CaptionText property of the DataGrid to the cells value.

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

// create the DataGrid and add it to the form

DataGrid dg = new DataGrid();

dg.Dock = DockStyle.Fill;

dg.CurrentCellChanged += this.CurrentCellChanged;

this.Controls.Add(dg);

// populate a datasource

Random random = new Random();

Point[] points = new Point[10];

for (int i = 0; i < points.Length; i++)

{

pointsIdea.X = random.Next();

pointsIdea.Y = random.Next();

}

dg.DataSource = points;

}

private void CurrentCellChanged(object sender, EventArgs e)

{

DataGrid dg = sender as DataGrid;

// set the caption to the value of the current cell ...

dg.CaptionText = dg[dg.CurrentCell.RowNumber, dg.CurrentCell.ColumnNumber].ToString();

}

}






Re: Windows Forms General need help with Datagrid's Captiontext

janue

oh, but I don't understand about c# at all. I would prefer visual basic codes, thx.




Re: Windows Forms General need help with Datagrid's Captiontext

Martin Xie - MSFT

Move this thread from BCL forum to Windows Form forum where is more appropriate.




Re: Windows Forms General need help with Datagrid's Captiontext

decyclone

Try this code :

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.DataSource = New MyDataSource().Source
End Sub

Private Sub DataGridView1_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
DataGridView1.Columns(e.ColumnIndex).HeaderText = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString()
End Sub
End Class

Class MyDataSource
Private mSource As DataTable
Public Property Source() As DataTable
Get
Return mSource
End Get
Set(ByVal value As DataTable)
mSource = value
End Set
End Property

Public Sub New()

Source = New DataTable()
Dim ColumnCount As Int32 = 5
For ColumnIndex As Int32 = 0 To ColumnCount
Source.Columns.Add(String.Format("Column {0}", ColumnIndex + 1))
Next

Dim RowCount As Int32 = 20
For RowIndex As Int32 = 0 To RowCount
Dim NewRow As DataRow = Source.NewRow()
For ColumnIndex As Int32 = 0 To ColumnCount
NewRow(ColumnIndex) = String.Format("Value {0} {1}", RowIndex, ColumnIndex)
Next
Source.Rows.Add(NewRow)
Next

End Sub
End Class






Re: Windows Forms General need help with Datagrid's Captiontext

mberseth

Here is the vb version ...

Public Class Form1
  Inherits Form
  ' Methods
  Public Sub New()
    Me.InitializeComponent
    Dim dg As New DataGrid
    dg.Dock = DockStyle.Fill
    AddHandler dg.CurrentCellChanged, New EventHandler(AddressOf Me.CurrentCellChanged)
    MyBase.Controls.Add(dg)
    Dim random As New Random
    Dim points As Point() = New Point(10 - 1) {}
    Dim i As Integer = 0
    Do While (i < points.Length)
      points(i).X = random.Next
      points(i).Y = random.Next
      i += 1
    Loop
    dg.DataSource = points
  End Sub

  Private Sub CurrentCellChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim dg As DataGrid = TryCast(sender,DataGrid)
    dg.CaptionText = dg.Item(dg.CurrentCell.RowNumber, dg.CurrentCell.ColumnNumber).ToString
  End Sub

  Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    If (disposing AndAlso (Not Me.components Is Nothing)) Then
      Me.components.Dispose
    End If
    MyBase.Dispose(disposing)
  End Sub

  Private Sub InitializeComponent()
    Me.components = New Container
    MyBase.AutoScaleMode = AutoScaleMode.Font
    Me.Text = "Form1"
  End Sub


  ' Fields
  Private components As IContainer = Nothing
End Class





Re: Windows Forms General need help with Datagrid's Captiontext

janue

hey people thx for the codes but I happened to find a easier method and it's exactly wad I wanted:

http://msdn2.microsoft.com/en-us/library/aa983712(VS.71).aspx