Hello,
Pls guide me as to what I can do to have the width of a column to expand automatically if the contents are large of a column. I have gone thru the various suggestions noted on this website but nothing has worked so far. Here is what I am using.
this.custGridCtrl.ForceInitialize();
gridViewCustomer.OptionsView.ColumnAutoWidth = false;
gridViewCustomer.BestFitColumns();
// colname_Customer is the column i like to adjust
colName_Customer.OptionsColumn.FixedWidth = false;
colName_Customer.BestFit();
Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Hello Tarang,
Thanks for your question. You code seems to be correct. I have attached a sample project, which works fine for me. Could you please change it, to demonstrate the issue, or create your own one, and attach it to your next message? I will review it, and provide you with a solution.
Thanks,
Uriah.
Hello Uriah,
thanks for the quick response. The culprit is CustomDrawCell method and would like to get your input how to handle this.
Thru the customdrawcell method, we are drawing a icon along with the value shown in col "code". All works fine but when I make the width of the column smaller, the column values overlaps with the next col value. It doesnt display "…" instead the value overlaps with the next column. I have attached a pic to show you.
private void GridViewCustomer_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
DevExpress.XtraGrid.Views.Grid.GridView currentView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
if (e.Column.FieldName == "Code")
{
Rectangle rcl = e.Bounds;
bool check = false;
e.DisplayText.Trim();
if (e.DisplayText == string.Empty)
{
check = false;
}
else
{
check = linqJobData.GetCustomerDeactiveStatus(e.DisplayText);
}
if (check == true)
{
e.Graphics.DrawImage(deletedIcon, rcl.X, rcl.Y);
}
else
{
e.Graphics.DrawImage(userIcon, rcl.X, rcl.Y);
}
rcl.X += 18;
e.Appearance.DrawString(e.Cache, e.DisplayText, rcl);
e.Handled = true;
}
}
Hi Tarang,
Please note that the GridView.BestFitColumns method takes cells display text into account. Changes made in the GridView.CustomDrawCell event handler do not participate in calculating the best width of a column since you can completely re-paint cell content in this event handler. If you wish to provide a grid column with a custom text, so, it is taken into account when calculating the best width, handle the GridView.CustomColumnDisplayText event instead.
Additionally, to properly draw a text in the GridView.CustomDrawCell event handler, you should use appropriate area (i.e. if you change the X coordinate, you need to also decrease the width of the text area). To display the ellipsis character at the end of the text, pass the StringFormat instance to the e.Appearance.DrawString method.
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) { GridCellInfo cellInfo = e.Cell as GridCellInfo; e.Graphics.DrawImage(SystemIcons.Error.ToBitmap(), new Rectangle(e.Bounds.X, e.Bounds.Y, 12, 12)); Rectangle rect = cellInfo.CellValueRect; int indent = 14; rect.X += indent; rect.Width -= indent; StringFormat format = new StringFormat(); format.Trimming = StringTrimming.EllipsisCharacter; e.Appearance.DrawString(e.Cache, e.DisplayText, rect, format); e.Handled = true; }
Also, I suggest that you display images in a separate unbound column and populate it by handling the GridView.CustomUnboundColumnData event.
Please let me know if you need any further assistance.
Thanks,
Svetlana