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

Copy data from RadGridComboBoxColumn

4 Answers 53 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Francois
Top achievements
Rank 1
Francois asked on 02 May 2011, 03:48 PM
Hi, I'm currently implementing Copy to Excel functionality and it works great for all but Combo/MultiCombo columns.

I'm trying to implement it in a generic way so I don't want to cast the data source of the combo and find the value from there. Is there a simple way to reach the Displayed data?

Thanks!

private string ConvertDataToString(bool onlySelectedRows)
{
    StringBuilder sbTextData = new StringBuilder();
 
    // Copy columns header
    foreach (GridViewColumn col in this.radGridView.Columns)
    {
        sbTextData.Append(col.HeaderText.Replace("\n", " ") + "\t");
    }
 
    sbTextData.Append("\n");
 
    // Convert only selected rows
    if (onlySelectedRows)
    {
        foreach (GridViewRowInfo row in this.radGridView.SelectedRows)
        {
            int i = 0;
            while (i < row.Cells.Count)
            {
                if (i > 0)
                {
                    sbTextData.Append("\t");
                }
 
                if (row.Cells[i].ColumnInfo is GridViewComboBoxColumn)
                {
                    // Obtain displayed data
                }
                else if (row.Cells[i].Value == null)
                {
                    sbTextData.Append(string.Empty);
                }
                else
                {
                    sbTextData.Append(row.Cells[i].Value.ToString());
                }
 
                i++;
            }
            sbTextData.Append("\n");
        }
    }
    else
    {
        foreach (GridViewRowInfo row in this.radGridView.Rows)
        {
            int i = 0;
            while (i < row.Cells.Count)
            {
                if (i > 0)
                {
                    sbTextData.Append("\t");
                }
 
                if (row.Cells[i].Value == null)
                {
                    sbTextData.Append(string.Empty);
                }
                else
                {
                    sbTextData.Append(row.Cells[i].Value.ToString());
                }
 
                i++;
            }
            sbTextData.Append("\n");
        }
    }
 
    return sbTextData.ToString();
}

4 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 03 May 2011, 08:23 AM
Hello Francois,

This is not that easy to accomplish and sadly there is no direct access to that text.

But there is a not so nice way to do this, please take a look at your revised example:

For this to work you have to register for the CellFormatting event and put in the Tag for that cell the text of the current cell element.
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    e.Row.Cells[e.ColumnIndex].Tag = e.CellElement.Text;
}

Ok, now this solved part of the problem, because the grid is virtualized (so just the visible cells are loaded) we will need to force the grid to scroll trough all of the rows before performing the actual copy operation (the downside of this is that this will lead to a slower copy operation, but the upside is that you make sure you always have the right values in the grid...)

private string ConvertDataToString(bool onlySelectedRows)
{
    var currentRow = radGridView1.CurrentRow;
    using (radGridView1.DeferRefresh())
    {
        foreach (var row in radGridView1.Rows)
        {
            radGridView1.TableElement.ScrollToRow(row);
 
        }
    }
 
    radGridView1.CurrentRow = currentRow;
    if (currentRow == radGridView1.Rows.First())
    {
        radGridView1.TableElement.ScrollTo(1, 1);
        radGridView1.TableElement.ScrollTo(0, 0);
    }
    else
    {
        currentRow.EnsureVisible();
    }
 
 
    StringBuilder sbTextData = new StringBuilder();
 
    // Copy columns header
    foreach (GridViewColumn col in this.radGridView1.Columns)
    {
        sbTextData.Append(col.HeaderText.Replace("\n", " ") + "\t");
    }
 
    sbTextData.Append("\n");
 
    // Convert only selected rows
    if (onlySelectedRows)
    {
        foreach (GridViewRowInfo row in this.radGridView1.SelectedRows)
        {
            int i = 0;
            while (i < row.Cells.Count)
            {
                if (i > 0)
                {
                    sbTextData.Append("\t");
                }
 
                if (row.Cells[i].ColumnInfo is GridViewComboBoxColumn)
                {
                    // Obtain displayed data
                }
                else if (row.Cells[i].Tag == null)
                {
                    sbTextData.Append(string.Empty);
                }
                else
                {
                    sbTextData.Append(row.Cells[i].Tag.ToString());
                }
 
                i++;
            }
            sbTextData.Append("\n");
        }
    }
    else
    {
        foreach (GridViewRowInfo row in this.radGridView1.Rows)
        {
            int i = 0;
            while (i < row.Cells.Count)
            {
                if (i > 0)
                {
                    sbTextData.Append("\t");
                }
 
                if (row.Cells[i].Tag == null)
                {
                    sbTextData.Append(string.Empty);
                }
                else
                {
                    sbTextData.Append(row.Cells[i].Tag.ToString());
                }
 
                i++;
            }
            sbTextData.Append("\n");
        }
    }
 
    return sbTextData.ToString();
}

Sorry about the extra scrolling if it's the first row but apparently there is a problem with scrolling to the first row after DeferRefresh() or BeginUpdate() / EndUpdate() has been used...

Or you can just use the cleaner version without DeferRefresh, which from my tests takes roughly the same time to process:
private string ConvertDataToString(bool onlySelectedRows)
{
    foreach (var row in radGridView1.Rows)
    {
        radGridView1.TableElement.ScrollToRow(row);
    }
 
    radGridView1.CurrentRow.EnsureVisible();
 
    StringBuilder sbTextData = new StringBuilder();
 
    // Copy columns header
    foreach (GridViewColumn col in this.radGridView1.Columns)
    {
        sbTextData.Append(col.HeaderText.Replace("\n", " ") + "\t");
    }
 
    sbTextData.Append("\n");
 
    // Convert only selected rows
    if (onlySelectedRows)
    {
        foreach (GridViewRowInfo row in this.radGridView1.SelectedRows)
        {
            int i = 0;
            while (i < row.Cells.Count)
            {
                if (i > 0)
                {
                    sbTextData.Append("\t");
                }
 
                if (row.Cells[i].ColumnInfo is GridViewComboBoxColumn)
                {
                    // Obtain displayed data
                }
                else if (row.Cells[i].Tag == null)
                {
                    sbTextData.Append(string.Empty);
                }
                else
                {
                    sbTextData.Append(row.Cells[i].Tag.ToString());
                }
 
                i++;
            }
            sbTextData.Append("\n");
        }
    }
    else
    {
        foreach (GridViewRowInfo row in this.radGridView1.Rows)
        {
            int i = 0;
            while (i < row.Cells.Count)
            {
                if (i > 0)
                {
                    sbTextData.Append("\t");
                }
 
                if (row.Cells[i].Tag == null)
                {
                    sbTextData.Append(string.Empty);
                }
                else
                {
                    sbTextData.Append(row.Cells[i].Tag.ToString());
                }
 
                i++;
            }
            sbTextData.Append("\n");
        }
    }
 
    return sbTextData.ToString();
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga

Telerik WinForms MVP
0
Accepted
Martin Vasilev
Telerik team
answered on 05 May 2011, 08:42 AM
Hello guys,

I would just like to suggest another way to achieve the Francois' requirement. The combo-box column's GetLookupValue method can be used for getting the displayed values. Here is the modified initial code:
StringBuilder sbTextData = new StringBuilder();
foreach (GridViewRowInfo row in this.radGridView1.Rows)
{
      
    int i = 0;
    while (i < row.Cells.Count)
    {
        if (i > 0)
        {
            sbTextData.Append("\t");
        }
        if (row.Cells[i].Value == null)
        {
            sbTextData.Append(string.Empty);
        }
        else if (row.Cells[i].ColumnInfo is GridViewComboBoxColumn)
        {
            // Obtain displayed data
            string lookupValueStr =
                ((GridViewComboBoxColumn)this.radGridView1.Columns[i]).GetLookupValue(row.Cells[i].Value).ToString();
            sbTextData.Append(lookupValueStr);
        }
        else
        {
            sbTextData.Append(row.Cells[i].Value.ToString());
        }
        i++;
    }
    sbTextData.Append("\n");
}

I hope this is helpful. Let me know if there are any additional questions.

Regards,
Martin Vasilev
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
0
Francois
Top achievements
Rank 1
answered on 16 Jun 2011, 07:20 PM
Thank's, it's working as expected. Here's the final version:

private string ConvertDataToString(bool onlySelectedRows)
{
    StringBuilder sbTextData = new StringBuilder();
 
    // Column header
    foreach (GridViewColumn col in this.radGridView.Columns)
    {
        sbTextData.Append(col.HeaderText.Replace("\n", " ") + "\t");
    }
 
    sbTextData.Append("\n");
 
    if (onlySelectedRows)
    {
        foreach (GridViewRowInfo row in this.radGridView.SelectedRows)
        {
            int i = 0;
            while (i < row.Cells.Count)
            {
                if (i > 0)
                {
                    sbTextData.Append("\t");
                }
 
                if (row.Cells[i].Value == null)
                {
                    sbTextData.Append(string.Empty);
                }
                else if (row.Cells[i].ColumnInfo is GridViewComboBoxColumn)
                {
                    // Obtain displayed data
                    var col = this.radGridView.Columns[i] as GridViewComboBoxColumn;
                    string lookupValueStr = col.GetLookupValue(row.Cells[i].Value).ToString();
                    sbTextData.Append(lookupValueStr);
                }
                else
                {
                    sbTextData.Append(row.Cells[i].Value.ToString());
                }
 
                i++;
            }
            sbTextData.Append("\n");
        }
    }
    else
    {
        foreach (GridViewRowInfo row in this.radGridView.Rows)
        {
            int i = 0;
            while (i < row.Cells.Count)
            {
                if (i > 0)
                {
                    sbTextData.Append("\t");
                }
 
                if (row.Cells[i].Value == null)
                {
                    sbTextData.Append(string.Empty);
                }
                else if (row.Cells[i].ColumnInfo is GridViewComboBoxColumn)
                {
                    // Obtain displayed data
                    var col = this.radGridView.Columns[i] as GridViewComboBoxColumn;
                    string lookupValueStr = col.GetLookupValue(row.Cells[i].Value).ToString();
                    sbTextData.Append(lookupValueStr);
                }
                else
                {
                    sbTextData.Append(row.Cells[i].Value.ToString());
                }
 
                i++;
            }
            sbTextData.Append("\n");
        }
    }
 
    return sbTextData.ToString();
}
0
Martin Vasilev
Telerik team
answered on 21 Jun 2011, 04:15 PM
Hi Francois,

Thank you very much for sharing your code with us. Do not hesitate to contact me again if there are any other questions.

Kind regards,
Martin Vasilev
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
Tags
GridView
Asked by
Francois
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Martin Vasilev
Telerik team
Francois
Top achievements
Rank 1
Share this question
or