Solved

DataGridView DataSource BindingList?

Posted on 2011-09-09
4
1,113 Views
Last Modified: 2012-05-12
I have a datagridview bound to a bindinglist, however the datagridview only shows a char count of 9 digits of the total, Not the actual total number.
I'm trying to eliminate using a dataset/datatable and just add columns to the datagridview dynamically. The datagridview will only have one row showing without column or row headers.

          BindingList<string> blist = new BindingList<string>();

          foreach (DataColumn col in DataSet1.Tables[0].Columns)
          {
                 string total = DataSet1.Tables[0].Compute("Sum(" + col.ColumnName + ")", "ID > 0").ToString();
                 blist.Add(total);
          }
         
          dataGridView1.AutoGenerateColumns = true;
          dataGridView1.DataSource = blist;

0
Comment
Question by:pointeman
4 Comments
 

Expert Comment

by:mihak1979
ID: 36512189
Add dataGridView1.DataBind()
0
 

Author Comment

by:pointeman
ID: 36513081
Windows Form not ASP...
0
 
LVL 15

Expert Comment

by:x77
ID: 36513214
I do´nt see the advantage from DataBound Dgv for this situation.

Not DataBound Dgv is too easy to manage.

1 - Generate an array of Columns you want to compute and add column names to Dgv:

   Dim Cnames = String.Split("Valor1,Valor2,Valor3,Valor4") 'Valor1 ... are columns to compute
   Dim Values (cnames.length -1)  as double ' To Store Totals
   For each name in Cnames
         dgv.columns.add( new DataGridviewTextBoxcolumn(Name) with {.Width = 80, .Format= ....}
   next

2 - Compute all values and add to Dgv:

   for earch row in myTable.Select("id>0")
        for n=0 to cnames.length -1
             dim v = row(cnames(n))
             if v isnot dbnull.value then values(n) += ctype(v,double)
        next
   next
   Dgv.Rows.clear
   Dgv.Rows.Add (values)

- I wrote code on Vb, but I think concept is clear.

   
0
 
LVL 5

Accepted Solution

by:
morgulo earned 500 total points
ID: 36513287
I think you should create model class:
public class StringValue{
public string Value{get;set;}
}

Open in new window

and use this:
BindingList<StringValue> blist = new BindingList<StringValue>();

          foreach (DataColumn col in DataSet1.Tables[0].Columns)
          {
                 string total = DataSet1.Tables[0].Compute("Sum(" + col.ColumnName + ")", "ID > 0").ToString();
                 blist.Add(new StringValue(){Value=total});
          }
         
          dataGridView1.AutoGenerateColumns = true;
          dataGridView1.DataSource = blist; 

Open in new window


or you can use linq:
dataGridView1.DataSource = blist.Select(x => new { Value = x }).ToList();

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
2 questions 10 25
Cant save 3D 4 19
Call windows 10 virtual keyboard from windows forms app 2 34
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question