Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Another newb LINQ question
Message
De
30/03/2011 03:33:29
 
 
À
29/03/2011 21:57:54
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01505332
Message ID:
01505459
Vues:
46
Hi,
It works if you reference the System.Linq namespace (and this *was* a Linq question :-} ) to pick up the IEnumerable.Contains() extension method.

Likewise for the adding option:
foreach (string s in new string[]{"ColumnA","ColumnB","ColumnC"})
            {
               dgvActiveBlocks.Columns.Add (new DataGridViewTextBoxColumn() { Name = s });
            }
>Nice Viv, I like it!! However, you were close, but no cigar. Need to use a List< string > instead of string[]:
>
>
>foreach (DataGridViewColumn col in dgvActiveBlocks.Columns)
>            {
>                col.Visible = new List<string>() {
>                    "PaymentBlock",
>                    "RaisedBy",
>                    "BlockDescription",
>                    "etc" }
>                    .Contains(col.Name);
>            }
>
>
>~~Bonnie
>
>
>
>
>>Your first option reminds me of a method I've used to achieve the same thing in one pass. Something like:
foreach (DataGridViewColumn col in dgvActiveBlocks.Columns)
>>            {
>>                col.Visible = new string[] {
>>                    "PaymentBlock",
>>                    "RaisedBy",
>>                    "BlockDescription",
>>                    "etc" }
>>                    .Contains(col.Name);
>>            }
Shorter, but dunno if it's quicker :-}
>>
>>>The real question is why do you want to populate your grid this way? Wouldn't binding it by setting the DataSource be better? By doing this, it's easy to add rows to your grid by simply adding rows to your bound DataTable, which makes more sense.
>>>
>>>
>>>this.dgvActiveBlocks.DataSource = this.dsBlocks.Tables[0];
>>>
>>>
>>>If you want all columns in that table to be in your grid, simply set
>>>
>>>
>>>this.dgvActiveBlocks.AutoGenerateColumns = true;
>>>
>>>
>>>If you don't want all columns, then you have a couple of options for getting the proper columns to display in your grid:
>>>
>>>You can either leave AutoGenerateColumns set to true and then hide all the columns in a loop & then set the ones you want to visible:
>>>
>>>
>>>foreach (DataGridViewColumn col in this.dataGridView1.Columns)
>>>{
>>>	col.Visible = false;
>>>}
>>>this.dataGridView1.Columns["PaymentBlock"].Visible = true;
>>>this.dataGridView1.Columns["BlockDescription"].Visible = true;
>>>this.dataGridView1.Columns["RaisedBy"].Visible = true;
>>>this.dataGridView1.Columns["RaisedDate"].Visible = true;
>>>this.dataGridView1.Columns["RaisedDepartment"].Visible = true;
>>>this.dataGridView1.Columns["Status"].Visible = true;
>>>
>>>
>>>Or, you can set AutoGenerateColumns to false & add the columns programmatically:
>>>
>>>
>>>DataGridViewTextBoxColumn[] cols = new DataGridViewTextBoxColumn[6];
>>>cols[0] = new DataGridViewTextBoxColumn();
>>>cols[0].DataPropertyName = "PaymentBlock";
>>>cols[0].HeaderText = "Payment Block";
>>>cols[1] = new DataGridViewTextBoxColumn();
>>>cols[1].DataPropertyName = "BlockDescription";
>>>cols[1].HeaderText = "BlockDescription";
>>>// etc. for the rest of the columns
>>>this.dataGridView1.Columns.AddRange(cols);
>>>
>>>
>>>~~Bonnie
>>>
>>>
>>>
>>>>I currently populate a DataGridview with the following code
>>>>
>>>>
>>>>foreach (DataRow dr in this.dsBlocks.Tables[0].Rows)
>>>>{
>>>>    Object[] cells = {dr["PaymentBlock"].ToString(),
>>>>                              dr["BlockDescription"].ToString(),
>>>>                               dr["RaisedBy"].ToString(),
>>>>                                dr["RaisedDate"].ToString(),
>>>>                                dr["RaisedDepartment"].ToString(),
>>>>                                dr["Status"]};
>>>>
>>>>                    this.dgvActiveBlocks.Rows.Add(cells);
>>>>}
>>>>
>>>>
>>>>Can this be achieved using LINQ ?
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform