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

Add Dynamically RadCheckBoxElement in columns with RadGridView

2 Answers 49 Views
GridView
This is a migrated thread and some comments may be shown as answers.
laki
Top achievements
Rank 1
laki asked on 17 Mar 2011, 08:36 AM
string[] strArr = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "B", "C", "D", "E", "F", "21", "22", "23", "24", "25", "26", "27", "2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "B", "C", "D", "2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "B", "C", "D", "2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "B", "C", "D", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
 
private void CreateColumnRow()
        {
            DataTable data = new DataTable();
            data.Columns.Add("1");
            data.Columns.Add("2");
            data.Columns.Add("3");
            data.Columns.Add("4");
            data.Columns.Add("5");
             
            for (int i = 0; i < strArr.Length / 5; i++)
            {
                DataRow dr = data.NewRow();
                data.Rows.InsertAt(dr, 0);
            }
            int intSub = strArr.Length % 5;
            if (intSub > 0)
            {
                DataRow dr = data.NewRow();
                data.Rows.InsertAt(dr, 1);
            }
 
            radGridView1.DataSource = data;
 
            radGridView1.Columns[0].Width = 100;
            radGridView1.Columns[1].Width = 100;
            radGridView1.Columns[2].Width = 100;
            radGridView1.Columns[3].Width = 100;
            radGridView1.Columns[4].Width = 100;
        }
  
private void Form1_Load(object sender, EventArgs e)
        {
              
            CreateColumnRow();
            loadCheckBox();
  
        }
  
private void loadCheckBox()
{
      int z = 0;
      for (int i = 0; i < radGridView1.RowCount; i++)
            {
                for (int j = 0; j < radGridView1.ColumnCount; j++)
                {
                    radGridView1.Rows[i].IsCurrent = true;
                    radGridView1.Columns[j].IsCurrent = true;
                    if (radGridView1.CurrentColumn.Index == j && radGridView1.CurrentRow.Index == i)
                    {
                        RadCheckBoxElement check = null;
                        if (radGridView1.CurrentCell.Children.Count == 0)
                        {
                            check = new RadCheckBoxElement();
                            while (z < strArr.Length)
                            {
  
                                check.Name = "check_" + strArr[z];
                                check.Text = strArr[z];
  
                                radGridView1.CurrentCell.Children.Add(check);
                                z++;
                                break;
                            }
                        }
                        else
                        {
                            check = (RadCheckBoxElement)radGridView1.CurrentCell.Children[0];
                        }
                    }
                    if (z == strArr.Length)
                    {
                        break;
                    }
  
                }
                if (z == strArr.Length)
                {
                    break;
                }
            }
     radGridView1.Rows[0].IsCurrent = true;
     radGridView1.Columns[0].IsCurrent = true;
}

Hello all person in forum!

I send a picture. In picture, i write problem error me. You view my code, you will see code of ( string[] strArr ). If I add ( strArr.lengh < 30 ), this code run OK. Else, it will be problem.

Help me fix it ! Thanks all !

2 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Todorov
Telerik team
answered on 22 Mar 2011, 10:50 AM
Hi laki,

RadGridView  uses virtualization to display its data which means that the visual cells are reused to display different data items. In your case you are binding the grid to an empty data table and you are setting your data to the visual elements which breaks the virtualization logic.

Here is how you can achieve this:
string[] strArr = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "B", "C", "D", "E", "F", "21", "22", "23", "24", "25", "26", "27", "2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "B", "C", "D", "2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "B", "C", "D", "2", "3", "4", "5", "6", "7", "8", "9", "10", "A", "B", "C", "D", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
 
private void CreateColumnRow()
{
    DataTable data = new DataTable();
    data.Columns.Add("1");
    data.Columns.Add("2");
    data.Columns.Add("3");
    data.Columns.Add("4");
    data.Columns.Add("5");
 
    for (int i = 0; i < strArr.Length / 5; i++)
    {
        int startIndex = 5 * i;
        data.Rows.Add(strArr[startIndex], strArr[startIndex + 1], strArr[startIndex + 2], strArr[startIndex + 3], strArr[startIndex + 4]);
    }
    int intSub = strArr.Length % 5;
    if (intSub > 0)
    {
        object[] restItems = new object[intSub];
        int startIndex = strArr.Length - intSub;
        for (int i = startIndex; i < strArr.Length; i++)
        {
            restItems[i - startIndex] = strArr[i];
        }
 
        DataRow dr = data.NewRow();
        dr.ItemArray = restItems;
        data.Rows.Add(dr);
    }
 
    radGridView1.DataSource = data;
 
    radGridView1.Columns[0].Width = 100;
    radGridView1.Columns[1].Width = 100;
    radGridView1.Columns[2].Width = 100;
    radGridView1.Columns[3].Width = 100;
    radGridView1.Columns[4].Width = 100;
}
 
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
    CreateColumnRow();
}
 
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    foreach (RadElement child in e.CellElement.Children)
    {
        if (child is RadCheckBoxElement)
        {
            e.CellElement.Children.Remove(child);
            child.Dispose();
            break;
        }
    }
 
    if (e.CellElement.Value != null && e.CellElement.Value.ToString().Length > 0)
    {
        RadCheckBoxElement check = new RadCheckBoxElement();
        check.Name = "check_" + e.CellElement.Value;
        check.Text = e.CellElement.Value.ToString();
        e.CellElement.Text = "";
 
        e.CellElement.Children.Add(check);
    }
}

I hope this will help you. If you have further questions, feel free to ask.

All the best,
Ivan Todorov
the Telerik team
0
laki
Top achievements
Rank 1
answered on 23 Mar 2011, 03:51 AM
Thank you !
Tags
GridView
Asked by
laki
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
laki
Top achievements
Rank 1
Share this question
or