I have created a windows form application with just gridview control. My task is that to create/bind textbox and combobox in the same column
Consider the gridview contains two columns
row[0][0] contains combobox
row[1][0] contains textbox.
I was successful in creating the above said task.
But my problem is when datas are populated in the gridview such that scrol bar appears. if i scroll down and then scroll up the combobox in the row[0][0] will appear in the row[1][0] and
textbox in the row[1][0] will appear in the row[2][0]
I have attached the code bellow.
Thanks
Veda
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);
}
}
}