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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

editing in datagrid

Status
Not open for further replies.

RakhiKalra

Programmer
Jun 6, 2003
41
IN
Hi,

I am new to .net programming.i have been given an assignment to make a windows desktop application using c#.

I have a particular need to enter data in datagrid and edit the grid data as well.there is a country form in which a grid with 2 columns-Countryid and Country Name. Country id is hidden hence only one column-country Name, is shown to the user. apart from this grid i have 2 buttons-edit and add.
by default grid is made readonly(using table style).

when the user clicks on edit button , only the selected row shud become editable.as soon as focus is lost from that editable cell, it shud become uneditable.plus the edited data shud b saved to database.

when the user clicks on add button,an empty row shud b inserted into the grid and shud b editable.here also as soon as focus is lost from that cell, data is to b saved into the database.

i am not getting the following things-
-the events which shud b captured(apart from button_click).
-the properties to make selected cell(or row) editable and eneditable.

please help
Rakhi
 

Access the DataGridColumnStyle object of that column and set ReadOnly propoerty to true to disable edit and to false to enable the edit of that column.
Here is an example of events to be used:
Code:
public class MyForm : System.Windows.Forms.Form
{
	private bool m_MouseEvent=false;
	private System.Windows.Forms.DataGrid m_DataGrid;
	private System.Windows.Forms.DataGrid.HitTestInfo m_HitTestInfo;
        private void InitializeComponent()
	{
                //...
		this.m_DataGrid.MouseDown += new  		System.Windows.Forms.MouseEventHandler(this.m_DataGrid_MouseDown);
		this.m_DataGrid.Click += new System.EventHandler(this.m_DataGrid_Click);
		this.m_DataGrid.CurrentCellChanged += new System.EventHandler(this.m_DataGrid_CurrentCellChanged);
 
}
}

public void SetReadOnly (DataColumn dc)
{
  m_DataGrid1.TableStyles[0].GridColumnStyles[dc.ColumnName].ReadOnly = true;
}

private void m_DataGrid_CurrentCellChanged(object sender, System.EventArgs e)
{
	if (m_MouseEvent) 
	OnCellClick(m_DataGrid.CurrentCell.RowNumber,m_DataGrid.CurrentCell.ColumnNumber);
	m_MouseEvent=false;

}
private void m_DataGrid_Click(object sender, System.EventArgs e)
{
	if (m_MouseEvent && m_HitTestInfo.Type==System.Windows.Forms.DataGrid.HitTestType.Cell)
		OnCellClick(m_HitTestInfo.Row,m_HitTestInfo.Column);
	else m_MouseEvent =false;
}
private void m_DataGrid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
	m_MouseEvent=true;
	m_HitTestInfo=m_DataGrid.HitTest(e.X,e.Y);
}
private void OnCellClick(int vRow, int vColumn)
{
	m_MouseEvent=false;
	//Get Current DataGrid Cell contents (vRow, vColumn)
	if (m_HitTestInfo.Row >= m_Rows) return;   
	object vCell = m_DataGrid[vRow,vColumn];
        // Set the cell
        object vValue="blabla";
         m_DataGrid[vRow,vColumn]=vValue;

}
-obislavu-

 
Just a different question:

How do you get those buttons on a row in a datagrid?

So you get:

Name
----------
Patrick EDIT DELETE (buttons?)

Can someone help or point me in the right direction?

- Raenius

"Free will...is an illusion"
 
Raenius,
i have seen this thing done in ASP.Net but not in the desktop applications.
i guess u wud need to use any 3rd party component for grid features.



Cheers
Rakhi Kalra
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top