Solved

DataGridView DataSource BindingList?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

13 Experts available now in Live!

Get 1:1 Help Now