9 Replies - 11623 Views - Last Post: 03 August 2011 - 03:56 AM Rate Topic: ***** 1 Votes

#1 jsmithj_john  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-August 11

How to fill a dataGridView from DataRow array

Posted 02 August 2011 - 06:27 AM

SqlConnection cn = new SqlConnection("Connection String");
SqlDataAdapter da = new SqlDataAdapter("select * from customer", cn);
DataSet ds=new DataSet();
DataRow[] dr;
da.Fill(ds,"myCustomer");
string cName = textBox1.Text.ToUpper();
dr = ds.Tables["myCustomer"].Select("[Custome Name] Like '" + cName + "%'");


I want to populate my datagridview with these rows.

Is This A Good Question/Topic? 0
  • +

Replies To: How to fill a dataGridView from DataRow array

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9498
  • View blog
  • Posts: 35,845
  • Joined: 12-June 08

Re: How to fill a dataGridView from DataRow array

Posted 02 August 2011 - 06:38 AM

Okay - so what is blocking you from doing that?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5632
  • View blog
  • Posts: 12,084
  • Joined: 02-June 10

Re: How to fill a dataGridView from DataRow array

Posted 02 August 2011 - 06:42 AM

See FAQ #16


FAQ (Frequently Asked Questions - Updated July 2011

  • Q: My program doesn't work. I didn't show you any code or actually tell you the error message. How do I fix it?
    Spoiler


  • Q: How do I get Form 'A' to make a change or talk to Form 'B'


  • Q: I'm trying to make a password/login form and...
    A: Password handling tutorial


  • Q: I'm making a client/server chat program and .....
    A:Client/server chat tutorial


  • Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?


  • Q: I don't understand XML Serialization so I can save my information/class


  • Q:How do I get sound in my program?
    A: Adding sound to your C# application


  • Q: How do I use the serial port?
    A: Serial port communication


  • Q: How do I deploy my program / Make an installer?
    A: C# application deployment project


  • Q: How do I do compression or .zip files?
    A:

  • Q: How do I do multi-threading? Having a problem with cross-threading...


  • Q: I am making a calculator as my school homework project and ...
    A: Calculator tutorial


  • Q: I've been struggling with this for days/weeks and I can't figure it out and my professor is worthless and can't teach. Can someone here explain it to me?
    Spoiler


  • Q: How do I save some information like preferences, settings or other data?


  • Q: I want my textbox to offer suggestions as the user types.
    A: Creating an auto-complete textbox


  • Q: I want to write a program that does x, y and z with features for A, B, and C. {Notice there is no question in this question}
    Spoiler


  • Q: I need to code to do x... Someone tell me the code for y...
    Spoiler


  • Q: What is the best programming language? What programming language should I learn?
    Spoiler


  • Q: How do I get my program to programmatically simulate a mouse click or button press?
    A: Simulate mouse and keystroke (even to another application)


  • Q: I'm not really sure what I want to do with my future? Do you guys like programming? I think I kinda like math and games and computers? What should I do or study?
    A: Get experience while you find yourself


[*]Q: How do I use PInvoke and the Win32 api calls to get functions not available in .NET framework?
A: Using the Win32 API Tutorial

[*]Q: How can I have both a WinForms Windows app, and still have a Console window open?
Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: How to fill a dataGridView from DataRow array

Posted 02 August 2011 - 12:25 PM

View Postjsmithj_john, on 02 August 2011 - 07:27 AM, said:

[code]SqlConnection cn = new SqlConnection("Connection String");
SqlDataAdapter da = new SqlDataAdapter("select * from customer", cn);
DataSet ds=new DataSet();
DataRow[] dr;
da.Fill(ds,"myCustomer");
string cName = textBox1.Text.ToUpper();
dr = ds.Tables["myCustomer"].Select("[Custome Name] Like '" + cName + "%'");


I want to populate my datagridview with these rows.

Try this way

       SqlConnection cn = new SqlConnection("Connection String");
     SqlDataAdapter da = new SqlDataAdapter("select * from customers", cn);

     DataSet ds = new DataSet();
     DataRow[] dr;
     da.Fill(ds, "customers");
     //string cName = textBox1.Text.ToUpper();//original
     dr = ds.Tables["customers"].Select("[Custome Name] Like '" + cName + "%'");
     DataTable seldt = new DataTable("selected");//<-- put any name
     //copy structure and data from source table
     seldt = ds.Tables["customers"].Copy();
     //clear new table but kep structure
     seldt.Rows.Clear();
     //fill new table from array of datarows 
     foreach (DataRow row in dr)
     {
         seldt.ImportRow(row);

     }

     dataGridView1.DataSource = seldt.DefaultView;
     if (cn.State == ConnectionState.Open)
         cn.Close();

This post has been edited by fixo: 02 August 2011 - 12:30 PM
Reason for edit:: please use code tags

Was This Post Helpful? 1
  • +
  • -

#5 jsmithj_john  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-August 11

Re: How to fill a dataGridView from DataRow array

Posted 02 August 2011 - 03:19 PM

Thanks fixo. You are awesome. Thanks for your solution. If you donít mind I want to ask you one more doubt of mine on this topic. How to filter columns from these selected rows? For an example,
suppose there are 7 rows like [Customer Id], [Customer Name], [Customer Address], [Email Id], [Mobile No] etc. But I want to show only [Customer Name], [Customer Address] and [Email Id] from these selected rows in dataGridView. How to do that? Thanks in advance..
Was This Post Helpful? 0
  • +
  • -

#6 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: How to fill a dataGridView from DataRow array

Posted 02 August 2011 - 03:41 PM

Hi, buddy
Now is too late for mee,
Will be back in tomorrow only
Shortly, you can set all the columns you need in the select statement,
e.g. "Select [col1],[col2],[colN] from mytable"
or set any columns to be hidden if you wanted be to select
all of the columns,using datagridview properties
Was This Post Helpful? 1
  • +
  • -

#7 jsmithj_john  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-August 11

Re: How to fill a dataGridView from DataRow array

Posted 02 August 2011 - 04:25 PM

ya.. I did it with this code -
dataGridView1.Columns["Mobile No"].Visible = false;

Its hidden there in gridview.
But, still I want to know, how to do it with select method..
Thanks for your support.
Have a great night.
Was This Post Helpful? 0
  • +
  • -

#8 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: How to fill a dataGridView from DataRow array

Posted 03 August 2011 - 12:33 AM

I was a bit busy with my own, sorry for the belating
This way is a bit tricky but it's working for me:
     dr = ds.Tables["customers"].Select("[CompanyName] Like '" + cName + "%'");
     DataTable seldt = new DataTable();
     string[] visible = { "CompanyName", "Address" ,"Phone"};//<-- change columns on what ever you need

     DataColumn[] cols = new DataColumn[visible.Length];

         foreach (string col in visible)
     {
     seldt.Columns.Add(col);
     }

         foreach (DataRow row in dr)
          {
              DataRow newrow = seldt.NewRow();
              foreach (string col in visible)
         {           
            newrow[col] = row[col]; 
         }
         seldt.Rows.Add(newrow);
     }
    
     dataGridView1.DataSource = seldt.DefaultView;


Was This Post Helpful? 1
  • +
  • -

#9 jsmithj_john  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-August 11

Re: How to fill a dataGridView from DataRow array

Posted 03 August 2011 - 03:35 AM

awesome..
Ok, One Last Question, Is This Possible .....
Select * from fixo.

Thanks. Have a great day..
Was This Post Helpful? 0
  • +
  • -

#10 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: How to fill a dataGridView from DataRow array

Posted 03 August 2011 - 03:56 AM

You're welcome
Happy coding :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1