none
Datatable.select RRS feed

  • Question

  • SqlConnection Con=new SqlConnection("My connection string here");            
                SqlDataAdapter Da = new SqlDataAdapter();
                Da.SelectCommand = new SqlCommand("Select * from Contacts", Con);
                DataTable Dt = new DataTable();
                Da.Fill(Dt);
                Dt.Select("Select * from Contact where FName==John");
                GridView1.DataSource = Dt;
                GridView1.DataBind();

    Hi

    I dont know how I can filter my datatable with select command; could you please help me on this:

     Dt.Select("Select * from Contact where FName=John");

    does not work.

    Thanks


    • Edited by NewCoder61 Tuesday, October 23, 2012 4:27 AM
    Tuesday, October 23, 2012 4:25 AM

Answers

All replies

  • Hi,

    Try with DataView

    DataView dv=new DataView(Dt);
    dv.RowFilter="FName='John'";
    Dt=dv.ToTable();
    GridView1.DataSource = Dt;
    GridView1.DataBind();

    PS.Shakeer Hussain

    Tuesday, October 23, 2012 6:09 AM
  • Thanks for the response. I know there are many other alternatives, one of which is the one you mentioned and the other one is:

    Dt.DefaultView.RowFilter = "FName='John'";

    But I would like to know about DataTable.Select command. I would like to know how I can filter with this specific command.

    Thnaks:)


    • Edited by NewCoder61 Tuesday, October 23, 2012 6:25 AM
    Tuesday, October 23, 2012 6:24 AM
  • Hi,

    You can filter by using DataRow[] as below.

    DataRow[] dr=dt.Select("FName='John'");
    foreach (DataRow row in dr)
    {
    }


    PS.Shakeer Hussain

    Tuesday, October 23, 2012 7:50 AM
  •             SqlConnection Con = new SqlConnection("My Connection string here");
                SqlDataAdapter Da = new SqlDataAdapter();
                Da.SelectCommand = new SqlCommand("Select * from Contacts", Con);
                DataTable Dt = new DataTable();
                Da.Fill(Dt);
                //Dt.DefaultView.RowFilter = "FName='John'";
                DataRow[] Dr = Dt.Select("FName='John'");
                foreach (DataRow D in Dr)
                {
                    Dt.Rows.Add(D);
                }
                GridView1.DataSource = Dt;
                GridView1.DataBind();
            

    Many Thanks for your help. The bold line gives the following error:

    This row already belongs to this table.



    • Edited by NewCoder61 Tuesday, October 23, 2012 8:40 AM
    Tuesday, October 23, 2012 8:38 AM
  • Hi,

    Try this,

    Da.Fill(Dt);
    DataTable dt2=new DataTable();
    dt2=Dt.Clone();
                //Dt.DefaultView.RowFilter = "FName='John'";
                DataRow[] Dr = Dt.Select("FName='John'");
                foreach (DataRow D in Dr)
                {
                    dt2.Rows.Add(D);
                }
                GridView1.DataSource = dt2;
                GridView1.DataBind();


    PS.Shakeer Hussain


    Tuesday, October 23, 2012 9:36 AM
  • Hi guys,

    We can't add a datarow from one datatable to another. To copy the datarows into a datatable, we can use DataTable.ImportRow Method.

    DataTable dt2 = new DataTable();
    dt2 = dt.Clone();
    DataRow[] drs = dt.Select("FName='John'");
    foreach (DataRow D in drs)
    {
    
        dt2.ImportRow(D);
    }

    For more details, see http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q308909

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 24, 2012 7:15 AM
    Moderator