Datagrid Row Header Caption

J

Joey Lau

I'm a newbie to .NET CF. Does anyone know how to add row header
caption within one single datagrid? or I have to use two datagrids
(one for Row Header Label, and one for data binding)? Please help.
 
I

Ilya Tumanov [MS]

You can do that the same way as on desktop - by setting up
DataGridTableStyle/DataGridColumnStyle with whatever caption you'd like.

There are plenty of samples, e.g.:


http://groups.google.com/group/micr...Data+Grid+TableStyle&rnum=15#374c8edeb0cc90de

--
Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
 
J

Joey Lau

I have try the method inside the link, but it appears as column header
instead of row header. Actually, what i want to do is a 2-Dimension
table with column header and row header, the data will display inside
the grid. Please advise.
 
I

Ilya Tumanov [MS]

Oh, you need row captions, not column captions? Sorry, you can't do that.

As a workaround you can set RowHeadersVisible to false and add extra column
to data source with would simulate row captions.

The problem with that solution is what user can scroll it away.


--
Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
 
A

Arun

Here is a piece of code which writes values in RowHeader.
You need to override datagrid paint event for this.
Try this if your requirement is to write on Rowheader, otherwise you
can follow what IIya has suggested, but the only drawback there is the
first column cannot be frozen when you slide horizontally.

Hope this helps,

Cheers,
Arun.



//using System.Data.OleDb;

namespace DataGridRowText
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlServerCe;
using System.Data.Common;


public class Form1 : System.Windows.Forms.Form
{
private DataGridRowText.MyDataGrid dataGrid1;
//private Point pointInCell00;

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
//
// Form1
//
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Text = "Forestry Sample";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

public static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{

if(System.IO.File.Exists("test.sdf"))
{
System.IO.File.Delete("test.sdf");
SqlCeEngine eng = new SqlCeEngine("DataSource=test.sdf");
eng.CreateDatabase();
}
else
{
SqlCeEngine eng = new SqlCeEngine("DataSource=test.sdf");
eng.CreateDatabase();
}
string connString = "Provider = SQLOLEDB;Data Source= vdvd;User
Id=xxxx;Password =xxxxx;Connect Timeout=30;Initial
Catalog=Forestry;Trusted_Connection=False";
SqlCeRemoteDataAccess rda = new
SqlCeRemoteDataAccess("http://vdvd/sqlce/sscesa20.dll","DataSource=test.sdf");
rda.Pull("tbl_FORE_PPC_FormClass","select * from
table1",connString);
string sqlString = "SELECT * FROM table1";
SqlCeDataAdapter dataAdapter = null;
DataSet _dataSet = null;
try
{
SqlCeConnection con = new SqlCeConnection("DataSource=test.sdf");

// Connection object
//OleDbConnection connection = new OleDbConnection(connString);

dataAdapter = new SqlCeDataAdapter(sqlString,con);
// Create data adapter object
//dataAdapter = new OleDbDataAdapter(sqlString, connection);

// Create a dataset object and fill with data using data adapter's
Fill method
_dataSet = new DataSet();
dataAdapter.Fill(_dataSet, "orders");
con.Close();
}
catch(Exception ex)
{
MessageBox.Show("Problem with DB access-\n\n connection: "+
connString +"\r\n\r\n query: " + sqlString
+ "\r\n\r\n\r\n" + ex.ToString());
this.Close();
return;
}
dataGrid1.DataSource = _dataSet.Tables["orders"];//.DefaultView;
//set topleft corner point so we can locate the toprow
pointInCell00 = new Point(dataGrid1.GetCellBounds(0,0).X + 4,
dataGrid1.GetCellBounds(0,0).Y + 4);

}

private void dataGrid1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
int row = TopRow();
int yDelta = dataGrid1.GetCellBounds(row, 0).Height + 1;
int y = dataGrid1.GetCellBounds(row, 0).Top + 2;

CurrencyManager cm = (CurrencyManager)
this.BindingContext[dataGrid1.DataSource];

while(y < dataGrid1.Height - yDelta && row < cm.Count)
{
//get & draw the header text...
string text = string.Format("",row);
e.Graphics.DrawString(text, dataGrid1.Font, new
SolidBrush(Color.Black), 12, y);
y += yDelta;
row++;
}
}

public int TopRow()
{
DataGrid.HitTestInfo hti =
dataGrid1.HitTest(dataGrid1.GetCellBounds(0,0).X + 4,
dataGrid1.GetCellBounds(0,0).Y + 4);
return hti.Row;
}
}

public class MyDataGrid : System.Windows.Forms.DataGrid
{
protected override void
OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti = this.HitTest(e.X, e.Y);
if(hti.Type == DataGrid.HitTestType.RowResize)
{
return; //no baseclass call
}
base.OnMouseMove(e);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top