Re: Disabling Datagrid column by Stoitcho
Stoitcho
Wed Aug 16 10:16:56 CDT 2006
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.
--
HTH
Stoitcho Goutsev (100)
"shreelu" <shreelekhas@gmail.com> wrote in message
news:1155731689.704209.235890@b28g2000cwb.googlegroups.com...
> 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
>