I'm trying to solve a couple of problems with using a combobox within my RadGridView and not sure if I have taken the most appropriate methods. So in either scenario, the code below might help someone else, but if this is not the way it was intended or of I'm missing something please let me know. I would much prefer to have all the combobox cells show the combobox live instead of when I "hover" or "click" on them.
First Problem is I wanted all of my cells centered, they are dealing with numbers that look much better centered. Even though I set TextAlignment on the columns to MiddleCenter it did not seem to have any effect.
Second Problem is I have a combobox column in the grid that I have successfully databound through some trial and error. The combo box shows text only when looking at it from the grid perspective and that annoys me, I want it to always be in "Edit" mode.Every attempt to turn all of them into edit mode caused the application to crash, so I settled for only showing one at a time by doing it on the mousemove and cellbeginedit methods.
I added events for cellmousemove and cellbeginedit
Code for Cell Mouse move is below (note that since I am changing the Text Alignment I'm being forced to set the color back to solid?)
This activates the "Begin Edit" for the cell it's over if it has one of the values in my combobox.
private void calendar_grida_CellMouseMove(object sender, MouseEventArgs e) | |
{ | |
if (sender is GridDataCellElement) | |
{ | |
GridDataCellElement cInfo = (GridDataCellElement)sender; | |
string cellValue = cInfo.Value.ToString().ToLower(); | |
if (cellValue == "n/a" | | |
cellValue == "open" | | |
cellValue == "closed") | |
{ | |
cInfo.RowInfo.Cells["status_taken"].BeginEdit(); | |
} | |
} | |
} |
The problem now is if I change to another group it crashes the application with object reference not set to an object, to fix that I handle the cellBeginEdit event and cancel the beginedit if the currentcell is not the combobox
private void calendar_grida_CellBeginEdit(object sender, GridViewCellCancelEventArgs e) | |
{ | |
string cellValue = this.calendar_grida.CurrentCell.Value.ToString().ToLower(); | |
if (cellValue == "n/a" | | |
cellValue == "open" | | |
cellValue == "closed") | |
{ | |
return; | |
} | |
else | |
{ | |
e.Cancel = true; | |
} | |
} |