How to get the column width at runtime?

Hello,


I am using sfDataGrid in winforms App and I have a requirement to set width and left position of textboxes in relation to specific columns in sfDataGrid. I have method to implement this requirement but anytime I run it throws an exception: "Value was either too large or too small for an int32"


I realised during code walk through that the these columns values were : NaN. The method run at

sfDataGrid1_SizeChanged event.


The method is shown below:


private void TotalsPlacement()

{


var leftPos = sfDataGrid1.Columns[0].Width + sfDataGrid1.Columns[1].Width +

sfDataGrid1.Columns[2].Width + sfDataGrid1.Columns[3].Width + sfDataGrid1.Columns[4].Width

+ sfDataGrid1.Columns[5].Width + sfDataGrid1.Columns[6].Width;


txtDebit.Left = Convert.ToInt32(leftPos);

txtCredit.Left = txtCredit.Left + Convert.ToInt32(sfDataGrid1.Columns[7].Width);

txtCredit.Width = Convert.ToInt32(sfDataGrid1.Columns[7].Width);


txtBalance.Left = txtCredit.Left;

txtBalance.Width = txtCredit.Width;

}



Thanks in advance for your usual assistance



Best regards,

Paul Aziz


3 Replies

AR Arulpriya Ramalingam Syncfusion Team September 20, 2021 03:00 PM UTC

Hi Paul, 
 
Thank you for your interest in Syncfusion products. 
 
As per the current behavior of SfDataGrid, the default value of Width property for columns is NAN to compute proper widths when AutoSizing is enabled. To get proper width of columns, we suggest that you to use the ActualWidth property. We have modified the provided code snippet and please make use of the below code. 
 
Example code 
 
var leftPos = sfDataGrid1.Columns[0].ActualWidth + sfDataGrid1.Columns[1].ActualWidth + sfDataGrid1.Columns[2].ActualWidth +  
    sfDataGrid1.Columns[3].ActualWidth + sfDataGrid1.Columns[4].ActualWidth + sfDataGrid1.Columns[5].ActualWidth + sfDataGrid1.Columns[6].ActualWidth; 
 
textBox1.Left = Convert.ToInt32(leftPos); 
textBox1.Left = textBox1.Left + Convert.ToInt32(sfDataGrid1.Columns[6].ActualWidth); 
textBox1.Width = Convert.ToInt32(sfDataGrid1.Columns[6].ActualWidth); 
textBox2.Left = textBox1.Left; 
textBox2.Width = textBox1.Width; 
 
Please get back to us if you need any further assistance. 
 
Regards, 
Arulpriya R. 



PA Paul replied to Arulpriya Ramalingam September 21, 2021 01:25 PM UTC

Hi  Arulpriya,


Thanks for the reply. 


Your suggestion worked like magic. I am truly grateful. 


This is just by way of information. My method was not producing desired result when it was being called from  sfDataGrid1_SizeChanged event. So I changed the event to Form_SizeChanged event and that is it.


Best regards

Paul Aziz



VS Vijayarasan Sivanandham Syncfusion Team September 22, 2021 04:18 PM UTC

Hi Paul,

Thanks for the update.

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S


Loader.
Up arrow icon