Solved

DataGridView DataSource BindingList?

Posted on 2011-09-09
4
1,126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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!
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

739 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