namespace
WindowsFormsApplication3
{
public partial class Grid : Form
{
public Grid()
{
InitializeComponent();
}
private DataTable dt = new DataTable();
private void Grid_Load(object sender, EventArgs e)
{
dt.Columns.Add("Value");
DataRow dr = dt.NewRow();
dr[0] = "Test";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "Test1";
dt.Rows.Add(dr);
DataTable table = new DataTable();
table.Columns.Add(
"Item");
table.Columns.Add(
"Type");
table.Columns.Add(
"Buy/Sell");
Random r = new Random();
for (int i = 0; i < 5; i++)
{
if(i< 2)
table.Rows.Add(
"Row " + i, 1);
else
table.Rows.Add(
"Row " + i, 2);
}
this.radGridView1.DataSource = table;
radGridView1.Columns[1].IsVisible =
false;
}
private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
if (e.Row is GridDataRowElement && e.Column.HeaderText == "Buy/Sell")
{
string s = Convert.ToString(e.Row.RowInfo.Cells[1].Value);
if (s == "1")
{
e.CellElement =
new ComboboxCell(e.Column, e.Row,dt,"ComboBox");
}
else if (s == "2")
e.CellElement =
new TextboxCell(e.Column, e.Row, "Hello", "TextBox");
}
}
}
public class ComboboxCell : GridDataCellElement
{
RadComboBoxElement comboBox;
DataTable table;
string controlType = string.Empty;
string text;
public ComboboxCell(GridViewColumn column, GridRowElement rowElement, DataTable dataTable, string type) :
base(column, rowElement)
{
table = dataTable;
controlType = type;
}
protected override void SetContentCore(object value)
{
comboBox.SelectedItem = comboBox.FindItem(
Convert.ToString(value));
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
comboBox.SelectedIndexChanged +=
new EventHandler(comboBox_SelectedIndexChanged);
}
base.Dispose(disposing);
}
protected override void CreateChildElements()
{
if (controlType == "ComboBox")
{
comboBox =
new RadComboBoxElement();
comboBox.DropDownStyle = Telerik.WinControls.
RadDropDownStyle.DropDownList;
comboBox.DataSource = table;
comboBox.DisplayMember =
"Value";
this.Children.Add(comboBox);
}
}
}
public class TextboxCell : GridDataCellElement
{
RadTextBoxElement textBox;
string controlType = string.Empty;
string text;
public TextboxCell(GridViewColumn column, GridRowElement rowElement, string value, string type) :
base(column, rowElement)
{
text = value;
controlType = type;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
}
base.Dispose(disposing);
}
protected override void CreateChildElements()
{
textBox =
new RadTextBoxElement();
textBox.Text = text;
this.Children.Add(textBox);
}
protected override void SetContentCore(object value)
{
textBox.Text =
Convert.ToString(value);
}
}
}
this
.radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
But the problem with that is it automatically sizes the columns to be all equal which is not suitable for this application. And in this case giving each column a fixed length is also not an option as this application needs to run on several resolutions. When i tried using
this.radGridView1.MasterGridViewTemplate.BestFitColumns();
The columns only appear on part of the child form(ie if the user resizes the child form, the columns do not resize with it). So if you could please let me know if there is a workaround. Thanks in advance.
// |
// RadMaskedEditBox_Test |
// |
this.RadMaskedEditBox_Test.Culture = null; |
this.RadMaskedEditBox_Test.EnableKeyMap = true; |
this.RadMaskedEditBox_Test.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(20)))), ((int)(((byte)(107))))); |
this.RadMaskedEditBox_Test.Location = new System.Drawing.Point(205, 10); |
this.RadMaskedEditBox_Test.Mask = "N1"; |
this.RadMaskedEditBox_Test.MaskType = Telerik.WinControls.UI.MaskType.Numeric; |
this.RadMaskedEditBox_Test.Name = "RadMaskedEditBox_Test"; |
this.RadMaskedEditBox_Test.PlaceHolder = ' '; |
this.RadMaskedEditBox_Test.RightToLeft = System.Windows.Forms.RightToLeft.Yes; |
// |
// |
// |
this.RadMaskedEditBox_Test.RootElement.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(20)))), ((int)(((byte)(107))))); |
this.RadMaskedEditBox_Test.Size = new System.Drawing.Size(40, 20); |
this.RadMaskedEditBox_Test.TabIndex = 23; |
this.RadMaskedEditBox_Test.Text = "0.0"; |
this.RadMaskedEditBox_Test.Value = "0"; |
#region Patches for Telerik |
/// <summary> |
/// Max length control |
/// </summary> |
private void RadMaskedEditBox_Test_KeyPress(object sender, KeyPressEventArgs e) |
{ |
if (this.RadMaskedEditBox_Test.Text.Length == 5) |
e.Handled = true; |
} |
#endregion |
Me
.PhoneTypeBindingSource.DataSource = CMXApplication.LookUps.PhoneTypes
CType(Me.PhonesRadGridView.Columns("PhoneType"), Telerik.WinControls.UI.GridViewLookUpColumn).DataSource = Me.PhoneTypeBindingSource.DataSource
Because of the nature of the custom objects I intercept the Cellformatting event, to display the name of the phone type
Private Sub PhonesRadGridView_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles PhonesRadGridView.CellFormatting
If e.CellElement.RowIndex >= 0 Then
If e.CellElement.ColumnIndex = 1 Then
e.CellElement.Text =
CType(PhonesBindingSource(e.CellElement.RowIndex), FTS.CMX.CMXModel.Phone).PhoneType.Name
End If
End If
End Sub
so far so good. When I edit the grid row I select a different phonetype in my GridViewLookUpColumn (PhoneType). I need to assign a phonetype from the GridViewLookUpColumn to my phone. What I think I need to do is this, capture the CellEndEdit value from the GridViewLookUpColumn and assign a phonetype to my phone.
When I query the GridViewLookUpColumn it is not the selected value from my grid
Can you give me pointers on how to achieve the desired results?
Private Sub PhonesRadGridView_CellEndEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles PhonesRadGridView.CellEndEdit
Dim Phone As FTS.CMX.CMXModel.Phone = ContactProxy.Contact.Phones(PhonesRadGridView.CurrentRow.ViewInfo.CurrentIndex)
If PhonesRadGridView.CurrentRow.Cells(0).Value = String.Empty Then
If Phone.EntityState = EntityState.Added Then
PhonesRadGridView.Rows.RemoveAt(ContactProxy.Contact.Phones.Count - 1)
ContactProxy.Contact.Phones.Remove(Phone)
End If
Else
If e.ColumnIndex = 1 Then 'assign the phone type to the phone
'CType(Me.PhonesRadGridView.CurrentCell, Telerik.WinControls.UI.GridComboBoxCellElement).Text
Debug.WriteLine(
CType(Me.PhonesRadGridView.CurrentCell, Telerik.WinControls.UI.GridComboBoxCellElement).Value)
End If
End If
End Sub
Thanks P