Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Another newb LINQ question
Message
De
29/03/2011 10:21:42
 
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:
01505347
Vues:
70
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform