This is a migrated thread and some comments may be shown as answers.

GridView control Issue

1 Answer 67 Views
GridView
This is a migrated thread and some comments may be shown as answers.
VEDA VIDVA NARAYANAN SATTANATHAN
Top achievements
Rank 1
VEDA VIDVA NARAYANAN SATTANATHAN asked on 18 Dec 2008, 10:29 AM
 Hi,
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);

 

}

}

}

1 Answer, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 19 Dec 2008, 05:08 PM
Hi VEDA VIDVA NARAYANAN SATTANATHAN,

Thank you for contacting us.

Probably you experience the glitch because RadGridView uses virtualization and reuses its cells when invalidating. More on virtualization you can find in the documentation.

In your CellCreate event you have to add code for all possible values in column with index 1, not only for "1" and "2". If this does not resolve the issue, please send me a small example application that demonstrates it. It will help me to investigate your case in details and provide you with further assistance.
 

Greetings,
Martin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
VEDA VIDVA NARAYANAN SATTANATHAN
Top achievements
Rank 1
Answers by
Martin Vasilev
Telerik team
Share this question
or