Disabling Datagrid column

S

shreelu

Hi
I have Datagrid with three columns pulled from database,
I have edit functionality in which only one column should be editable,
how do i make other two columns readOnly
next is Add New functionality in which the user should be allowed only
to add a new record and not edit any other record

how do i do this, if anyone has any idea

thanks
shreelekha
 
S

Stoitcho Goutsev \(100\)

Shreelu,

What you need to do in order to make a column read-only is to create a new
DataGridColumnStyle and override the Edit method. In the override don't call
the base class. This will make the column that this style is used for
read-only.

Here is some code snipped of how to do that.


This the read-only column style
public class NonEditableCellColumn : DataGridTextBoxColumn
{
protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string
instantText, bool cellIsVisible)
{
// Don't call the base class, so the column will be read-only
}


}


Here is how to use the column style with a datagrid. This code makes a
column bound to the db-column 'SALES' read only.

public Form1()
{
InitializeComponent();
CurrencyManager cm =
(CurrencyManager)this.dataGrid1.BindingContext[this.dataSource];
DataGridTableStyle tableStyle = new DataGridTableStyle(cm);

NonEditableCellColumn column = new NonEditableCellColumn();
column.MappingName = "SALES";
column.HeaderText = "SALES";
tableStyle.GridColumnStyles.Remove(tableStyle.GridColumnStyles["SALES"]);
tableStyle.GridColumnStyles.Add(column);
this.dataGrid1.TableStyles.Add(tableStyle);
}


As far as the second part of your question goes, I believe you can get the
DefaultView of the DataTable you are binding to and set all AllowXXXX
property, but AllowNew to false.
 

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