Solved

DataGridView DataSource BindingList?

Posted on 2011-09-09
4
1,107 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ - C# to VB convertion 12 45
Export import database 4 40
Not showing JavaScript in the list 5 35
Exit the loop 4 31
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now