Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataGridView DataSource BindingList?

Posted on 2011-09-09
4
Medium Priority
?
1,131 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

730 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