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);
}
}
}