Set individual cell not to allow edit

I have a small issue. The DataGrid is sourced from a DataTable. for Instance

id   Country   First Name   Last Name     Name                    Date
1    Ghana         John               Mark                                             2/2/1994
2    Canada                                                   Karl Michael          3/5/2020
3    US                                                          Biden Johnson       5/5/2005
4    Spain         Samuel          Jones                                             4/3/4464

From the above, my main aim is that if id 1 is to be edited, column "Name" should not allow editing. If id 3 is to be edited, Columns ("First Name" and "Last Name") shouldn't be edited.
How do I achieve any of this, or which one would be the easiest?
a)     Merging the empty cells; or
b)     Disallowing the empty cells from editing.

13 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team April 14, 2021 06:35 PM UTC

Hi Olayinka,

Thank you for contacting Syncfusion Support.

SfDataGrid is not a cell bound control.it is Data bound control. In SfDataGrid.AllowEditing property enable and disable for Column or Grid-based but we do not enable or disable AllowEditing property for a particular cell or row in SfDataGrid. But you can cancel the editing by using SfDataGrid.CurrentCellBeginedit event.

In SfDataGrid value-based editing disable or readonly for currentcell achieve by using SfDataGrid.CurrentCellBeginEdit. Please refer the below code snippet, 
Code snippet C# for Editing canceled in SfDataGrid: 
sfDataGrid1.CurrentCellBeginEdit += SfDataGrid1_CurrentCellBeginEdit; 
 
private void SfDataGrid1_CurrentCellBeginEdit(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellBeginEditEventArgs e) 
        { 
            //get the DataRow value 
            var dataRow = (e.DataRow.RowData as DataRowView).Row; 
 
            //get the currentcell value 
            var cellValue = dataRow[e.DataColumn.GridColumn.MappingName].ToString(); 
 
            //Here customized the cell value based cancel the Editing 
            if (string.IsNullOrEmpty(cellValue)) 
            { 
                e.Cancel = true; 
            }  
        } 

For more information, please refer the below UG link,

UG Link:
https://help.syncfusion.com/windowsforms/datagrid/editing#cancel-the-editing-of-the-current-cell 
Please let us know, if you require further assistance on this. 
Regards,
Vijayarasan S
 


Marked as answer

OL Olayinka April 15, 2021 11:09 AM UTC

thanks. That's better.
Which means it can't set each cell with different back color either, can it?
I'm sorry to ask this, is it possible to get the sum of a column into a GroupCaptionTextFormat?
Also, how can one change the format of a Column which is a string to double and vice versa?,


VS Vijayarasan Sivanandham Syncfusion Team April 16, 2021 06:28 PM UTC

Hi Olayinka,

Thanks for the update.

Please find answer for your queries below 
Queries 
Solutions 
 
Which means it can't set each cell with different back color either, can it? 

you can apply the background color for cell based on cell value can be achieve by using SfDataGrid.QueryCellStyle event. . For more information, please refer the below UG link,

UG Link:
https://help.syncfusion.com/windowsforms/datagrid/conditionalstyling

https://help.syncfusion.com/windowsforms/datagrid/styling#styling-record-cell 
 
 
 
I'm sorry to ask this, is it possible to get the sum of a column into a GroupCaptionTextFormat? 
 

We are little unclear with your scenario. Can you please provide the more information related to your query?

Can you please share us below things?
        
        1.
Provide more details about your scenario with illustrations?    
        2.
Provide more details about your scenario with image illustrations?   
       
 
It will be helpful for us to check on it and provide you the solution at the earliest. 
 
 
Also, how can one change the format of a Column which is a string to double and vice versa?, 
 
 
We are little unclear with your scenario. Can you please provide the more information related to your query?

Can you please share us below things?
        
        1.
Provide more details about your scenario with illustrations?    
        2.
Provide more details about your scenario with image illustrations?   
       
 
It will be helpful for us to check on it and provide you the solution at the earliest. 
 
 
Please let us know if you have any concerns in this. 

Regards,
Vijayarasan S
 



OL Olayinka April 17, 2021 01:50 PM UTC

from the sample above, the Available Qty was sourced from the database which was formatted as double. Now, upon getting to the datagrid, it automatically converted the numbers to 2 decimal places. There are some figures that are whole numbers while some are in 3 decimal places, how do I make it show that the actual figure. i.e. If the Qty is 3 the DataGrid should display 3 and if it is 1.125, it should display as 1.125


VS Vijayarasan Sivanandham Syncfusion Team April 19, 2021 01:17 PM UTC

Hi Olayinka,

Thanks for the update.

Your requirement can be achieved by setting value to GridNumericColumn.NumberFormatInfo.NumberDecimalDigits property in SfDataGrid. Please refer the below code snippet, 
  numberFormat = new NumberFormatInfo(); 
  //Set the decimal digit  
  numberFormat.NumberDecimalDigits = 3; 
  //trigger the AutoGenenerate column event  
  sfDataGrid1.AutoGeneratingColumn += SfDataGrid1_AutoGeneratingColumn; 
 
private void SfDataGrid1_AutoGeneratingColumn(object sender, Syncfusion.WinForms.DataGrid.Events.AutoGeneratingColumnArgs e) 
{ 
            //check the Column 
            if (e.Column.MappingName == "OrderID") 
            { 
                e.Column.HeaderText = "Available Qty"; 
                //Set the NumberFormatInfo 
                (e.Column as GridNumericColumn).NumberFormatInfo = numberFormat;                
            } 
} 
For more information, please refer the below UG link,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/columntypes#data-formatting 
Please let us know, if you require further assistance on this. 

Regards,
Vijayarasan S 



OL Olayinka April 22, 2021 05:09 PM UTC

this did not solve what I wanted. This question is ....

 If the Qty is the DataGrid should display 3 and if it is 1.125, it should display as 1.125

using that code makes 3 to become 3.000


VS Vijayarasan Sivanandham Syncfusion Team April 24, 2021 06:19 PM UTC

Hi Olayinka,

Thanks for the update.

your requirement can be achieved by using SfDataGrid.DrawCell event. Please refer the below code snippet, 
sfDataGrid1.DrawCell += SfDataGrid1_DrawCell;

 
private void SfDataGrid1_DrawCell(object sender, Syncfusion.WinForms.DataGrid.Events.DrawCellEventArgs e) 
{ 
            //check the Column 
            if (e.Column.MappingName == "OrderID") 
            { 
                //displaye the number customized 
                e.DisplayText =  (Math.Abs(Convert.ToDouble(e.DisplayText))).ToString();                 
            } 
} 
Please let us know, if you require further assistance on this. 
Regards,
Vijayarasan S 



OL Olayinka April 28, 2021 09:37 PM UTC

this did not work either


VS Vijayarasan Sivanandham Syncfusion Team April 29, 2021 09:41 AM UTC

Hi Olayinka, 

Thanks for the update. 

Based on provided information we have checked the sample with provided solution. it is working fine as expected as your scenario. Please find the tested sample and video demo from our end in the below link,

Sample Link: https://www.syncfusion.com/downloads/support/forum/164504/ze/ModifiedSample-1341834398 
if you still facing the same issue? If yes, please modify the sample based on your scenario. 

It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Vijayarasan S   


OL Olayinka April 29, 2021 03:23 PM UTC

When I applied it to my code, the datagrid displayed crossed red lines - no data was displayed. 
Please, note that the datatable is from the database


VS Vijayarasan Sivanandham Syncfusion Team May 1, 2021 06:42 AM UTC

Hi Olayinka,

Thanks for the update.

Based on provided information we have created the sample with Database binded and try to replicate your reported problem
and unable to replicate the problem from our end in simple sample. it is working fine as expected as your scenario. Please find the tested sample and video demo from our end in the below link,

Sample Link: https://www.syncfusion.com/downloads/support/forum/164504/ze/DataBaseBindedSample-1747468341

Video Link: https://www.syncfusion.com/downloads/support/forum/164504/ze/DecimalDigitsDisplay-1498688956

if you still facing the same issue? If yes, please modify the sample based on your scenario.

It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Vijayarasan S  
 



OL Olayinka May 11, 2021 04:21 PM UTC

Many thanks.

this line of code was the joker.

if(!string.IsNullOrEmpty( e.DisplayText))

possibly because there were some cells that were empty



VS Vijayarasan Sivanandham Syncfusion Team May 12, 2021 03:21 PM UTC

Hi Olayinka,

Thanks for the update.

Based on provided information in above mentioned code snippet is used to avoid the exception that occurs when Empty or Null value passing in Convert.Double method. But this condition does not affect your provided input values in SfDataGrid. If an empty value passed in the cell its displays the empty value properly.

Sample Link:  https://www.syncfusion.com/downloads/support/forum/164504/ze/EmptyValueDisplayed-1886124223

Please let us know if you have any concerns in this.

Regards,
Vijayarasan S


Loader.
Up arrow icon