Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Datagrid & Where Clause

    Hi,

    I want to have a datagrid that has a Where Clause in each row.

    Is there a way to do this?

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Quote Originally Posted by CodeThis
    Hi,

    I want to have a datagrid that has a Where Clause in each row.

    Is there a way to do this?
    You have to be much more specific than this. Please expound on your question.

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CodeThis
    Hi,

    I want to have a datagrid that has a Where Clause in each row.

    Is there a way to do this?
    I have a table similiar to this....

    ID Student Class
    1 John Smith Algebra
    2 John Doe English
    3 Sally Smith English
    4 Nolan Ryan Algebra

    Below is what the Datagrid would produce

    John Smith Algebra
    John Doe English
    Sally Smith English
    Nolan Ryan Algebra


    **************

    I hope this makes more sense.

  • #4
    New Coder
    Join Date
    Oct 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    u can do this by using select method of table
    first u fill the dataset from backend database
    then use

    DataRow[] rr=dataset.Tables["table_name"].Select("ID<10");

    then show this into datagrid

    Quote Originally Posted by CodeThis
    I have a table similiar to this....

    ID Student Class
    1 John Smith Algebra
    2 John Doe English
    3 Sally Smith English
    4 Nolan Ryan Algebra

    Below is what the Datagrid would produce

    John Smith Algebra
    John Doe English
    Sally Smith English
    Nolan Ryan Algebra


    **************

    I hope this makes more sense.

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by handshakeit
    u can do this by using select method of table
    first u fill the dataset from backend database
    then use

    DataRow[] rr=dataset.Tables["table_name"].Select("ID<10");

    then show this into datagrid
    Can you provide some more info on how to use the DataRow. It sounds like this is what I need, but I am not sure how to use it.

    Thanks

  • #6
    New Coder
    Join Date
    Oct 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To show this in grid create a table "t"
    and then

    foreach(DataRow r in rr)
    {
    DataRow r1=t.NewRow();
    r1[0]=r[0];
    r1[1]=r[1];
    r1[2]=r[2];
    r1[3]=r[3];
    r1[4]=r[4];
    t.Rows.Add(r1);
    }
    dataGrid1.DataSource=t;


    I think this is messy code
    there should be some other way also

  • #7
    New Coder
    Join Date
    Dec 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by handshakeit
    To show this in grid create a table "t"
    and then

    foreach(DataRow r in rr)
    {
    DataRow r1=t.NewRow();
    r1[0]=r[0];
    r1[1]=r[1];
    r1[2]=r[2];
    r1[3]=r[3];
    r1[4]=r[4];
    t.Rows.Add(r1);
    }
    dataGrid1.DataSource=t;

    I think this is messy code
    there should be some other way also

    **************

    So how would you add this to the below code.

    <asp:datagrid id="Datagrid1" runat="server" DataKeyField="Vehicle">

    Columns>

    <asp:BoundColumn DataField="Class"></asp:BoundColumn>

    </Columns>

    <Columns>

    <asp:BoundColumn DataField="Teacher"></asp:BoundColumn>

    </columns>

    </asp:datagrid></form>

    </body

    </html>

    Private Sub page_load()

    IF (Not IsPostBack) Then

    BindData()

    End if

    End Sub

    Sub BindData()

    Dim objConn as New sqlConnection.....

    dim ds as dataset = new dataset

    dim adapter as new sqldataAdapter("Select * from Class;", objconn)

    adapter.Fill(ds, "ClassTableName")

    DataGrid1.DataSource = ds.tables("ClassTableName").defaultView

    DataGrid1.DataBind()

    objConn.Close()

    End Sub

  • #8
    New Coder
    Join Date
    Oct 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Private Sub page_load()

    IF (Not IsPostBack) Then

    BindData()

    End if

    End Sub

    Sub BindData()

    Dim objConn as New sqlConnection.....

    dim ds as dataset = new dataset

    dim adapter as new sqldataAdapter("Select * from Class;", objconn)

    adapter.Fill(ds, "ClassTableName")

    'declare a datarow array

    dim rr as []DataRow = new DataRow[ds.Tables["ClassTableName"].Rows.Count]

    rr=ds.Tables["ClassTableName"].Select("id<2")

    dim t as DataTable=new DataTable

    foreach(DataRow r in rr)
    {
    dim r1 as DataRow=t.NewRow();
    r1[0]=r[0];
    r1[1]=r[1];
    r1[2]=r[2];
    r1[3]=r[3];
    r1[4]=r[4];
    t.Rows.Add(r1);
    }
    dataGrid1.DataSource=t;

    DataGrid1.DataBind()

    objConn.Close()

    End Sub


    // there may some error in converting C# code in VB
    //I have very little idea of VB.Net
    //plz tell me if it works or not

    Thanx
    Last edited by handshakeit; 03-04-2006 at 06:43 AM.

  • #9
    New Coder
    Join Date
    Oct 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can do this as follows also
    this is better way

    DataTable dt = (DataTable)dataGrid1.DataSource;

    DataView dv = new DataView(dt);

    dv.RowFilter="condition for filtering";

    dataGrid1.DataSource=dv;

    thanx

  • #10
    New Coder
    Join Date
    Dec 2005
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think the method that you mentioned with the arrays is probably a better way to go, but this seems to do the job. I just haven't figured out the correct coding for the array methoed yet.

    'Views Setup

    Dim dvSmith as New DataView(ClassTableName)
    Dim dvJones as New DataView(ClassTableName)

    dvSmith.RowFilter = "Class = 'Eng101'"
    dvJones.RowFilter = "Class = "PSY101'"

    DataGrid1.datasource = dvSmith
    datagrid2.datasource = dvJones

    datagrid1.databind
    datagrid2.databind

  • #11
    New Coder
    Join Date
    Oct 2005
    Posts
    84
    Thanks
    0
    Thanked 0 Times in 0 Posts
    tell me where r u facing problem in first array method.......
    and also tell,
    Is the second one does not fufill ur requirement??


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •