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

Referring to columns in a typesafe manner

3 Answers 53 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 13 Jun 2011, 10:53 PM
I've run into a number of times now where I find myself having to refer to a column by its name, which rubs me the wrong way; for example, the following snippet to run through and check whether or not all of the selected cells are in the same column:

allDataCells = true;
foreach (GridViewCellInfo cell in commandGridView.SelectedCells)
{
    if (cell.ColumnInfo.Name != "columnData")
    {
        allDataCells = false;
        break;
    }
}

It bugs me that I'm using the text name rather than the actual column variable.  I know, in my example scenario, that the column variable is actually "gridViewTextBoxColumn8" (which could be a better name, I know).

Other than comparing on the name variables, which is a little better:

 

gridViewTextBoxColumn8.Name == cell.ColumnInfo.Name

...is there a "good" way to do this?  When given a GridViewCellInfo item, what's the type-safe and typo-safe way to compare the column?

Thanks!
Dave

3 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 16 Jun 2011, 11:34 AM
Hi Dave,

Thank you for contacting us. You can use the following solution to implement this:

bool allDataCells = true;
GridViewColumn current = null;
foreach (GridViewCellInfo cell in this.radGridView1.SelectedCells)
{
    if (current == null)
    {
        current = cell.ColumnInfo;
        continue;
    }
 
    if (current != cell.ColumnInfo)
    {
        allDataCells = false;
        break;
    }
}

I hope this information is useful. Let me know if you need further assistance.

Regards,

Julian Benkov
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
Dave
Top achievements
Rank 1
answered on 28 Jun 2011, 09:00 PM
Perhaps I am misunderstandnig it, but as I read your code it appears to check whether or not all selected cells are in the same column.  That's part of it, but my code (that I wish to improve) needs to check to see if they are all in a PARTICULAR column.  And that's the crux of my question, how to refer to that column without knowing the text name I used in the designer.

This question also comes up in how to define GridViewRelations and anything else where you need to refer to a specific column.  Since the columns are defined as local variables in the IntializeComponent() member (code generated by the designer), the variable name is long lost, so you cannot use

myDataColumn.Name

The best I can think of so far is to define a string once and use it in both places. 

string DataColumnName = "dataColumn";

Then in InitializeComponent:

myDataColumn.Name = DataColumnName

and then later in selection code

if (columnInfo.Name == DataColumnName)

but that's a bit of a hack.  Is there a way to have the designer not delcare the columns as local variables that go out of scope?   That'd solve it, and I doubt its any more inefficient, as there are certainly references to the columns held onto by the grid anyway, I'd imagine.
0
Julian Benkov
Telerik team
answered on 01 Jul 2011, 10:38 AM
Hi Dave,

For the scenarios where you have the a known reference to a specific column in your code, you can use it directly to process all your custom logic.

bool allDataCells = true;
GridViewColumn current = gridViewTextBoxColumn8;
foreach (GridViewCellInfo cell in radGridView1.SelectedCells)
{
    if (current != cell.ColumnInfo)
    {
        allDataCells = false;
        break;
    }
}
 
GridViewTemplate template = new GridViewTemplate();
GridViewTextBoxColumn childGridViewTextBoxColumn = new GridViewTextBoxColumn();
template.Columns.Add(childGridViewTextBoxColumn);
 
GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate, template);
relation.ParentColumnNames.Add(gridViewTextBoxColumn8.Name);
relation.ChildColumnNames.Add(childGridViewTextBoxColumn.Name);
radGridView1.Relations.Add(relation);


In cases when you know only the name of column you can get the reference to column and use it to have better performance.
bool allDataCells = true;
GridViewColumn current = radGridView1.Columns["MyColumn"];
foreach (GridViewCellInfo cell in radGridView1.SelectedCells)
{
    if (current != cell.ColumnInfo)
    {
        allDataCells = false;
        break;
    }
}
 
GridViewTemplate template = new GridViewTemplate();
GridViewTextBoxColumn childGridViewTextBoxColumn = new GridViewTextBoxColumn();
template.Columns.Add(childGridViewTextBoxColumn);
 
GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate, template);
relation.ParentColumnNames.Add(current.Name);
relation.ChildColumnNames.Add(childGridViewTextBoxColumn.Name);
radGridView1.Relations.Add(relation);

I hope this helps.

All the best,
Julian Benkov
the Telerik team
Registration for Q2 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting July 18th and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
Dave
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Dave
Top achievements
Rank 1
Share this question
or