Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Another newb LINQ question
Message
From
30/03/2011 11:23:14
 
 
General information
Forum:
ASP.NET
Category:
LINQ
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01505332
Message ID:
01505533
Views:
33
>It works if you reference the System.Linq namespace (and this *was* a Linq question :-} ) to pick up the IEnumerable.Contains() extension method.

Arrrggghhh! You're right! My little test app has several forms I use for testing stuff. Form1, which I use for almost everything, already references System.Linq. But Form4, which I use mainly for testing some DataGridView stuff, didn't have a System.Linq reference (which I didn't notice since I don't use that Form all that often). Thanks for point it out. Yesterday was a long day ... is that a good excuse? <g>

~~Bonnie




>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 ?
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform