Hi,

I need help binding the DataTable to the GridView.

Let's say I have this code

string filter = String.Format("title LIKE '{0}'", str);    
DataRow[] rows = dt.Select(filter, "title");


Gridview1.DataSource = ?;
Gridview1.DataBind();

I need to display only the filtered items in the GridView. What do I assign to DataSource? I tried rows but that gave me an error.

Thanks.

Recommended Answers

All 7 Replies

you can create dataview from your datatable. Then use RowFilter property of dataview to filter the data. and then assign your dataview to Gridview.DataSource property. I mean you can bind with dataview.


Hi,

I need help binding the DataTable to the GridView.

Let's say I have this code

string filter = String.Format("title LIKE '{0}'", str);    
DataRow[] rows = dt.Select(filter, "title");


Gridview1.DataSource = ?;
Gridview1.DataBind();

I need to display only the filtered items in the GridView. What do I assign to DataSource? I tried rows but that gave me an error.

Thanks.

Hi,

I need help binding the DataTable to the GridView.

Let's say I have this code

string filter = String.Format("title LIKE '{0}'", str);    
DataRow[] rows = dt.Select(filter, "title");


Gridview1.DataSource = ?;
Gridview1.DataBind();

I need to display only the filtered items in the GridView. What do I assign to DataSource? I tried rows but that gave me an error.

Thanks.

Hi

Just bind the grid like this ...

Gridview1.DataSource = dt.Select(filter, "title");
Gridview1.DataBind();

Mark as solved if it helps you!!!

I tried the code below but it did not work. The GridView is not populating.

Gridview1.DataSource = dt.Select(filter, "title");
      Gridview1.DataBind();

This is what my GridView looks like:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
      </asp:GridView>

I also tried DataView.

DataView view = dt.DefaultView;
      view.RowFilter = String.Format("title LIKE '{0}'", str);
      GridView1.DataSource = view;   
      GridView1.DataBind();

It works for the first time and that's it. Any idea what the problem might be? Thanks

I believe it's working now.
This is what I have now:

DataView view = new DataView(dt);
 view.RowFilter = String.Format("title LIKE '{0}'", str);
 GridView1.DataSource = view;
 GridView1.DataBind();

Now, I need to apply a function to 'view' before I bind it to the GridView. Something like this AsciiToString(view). Of course it's not working. Any idea how to apply it correctly? Thanks.

Here is the function:

private static string AsciiToString(string content)
    {
        //StringBuilder sbTemp = new StringBuilder(content.Length);
        string StrValue = "";
        while (content.Length > 0)
        {
            // convert each ASCII value to the actual character
            StrValue += System.Convert.ToChar(System.Convert.ToInt32(content.Substring(0, 3))).ToString();
            // Remove the converted value
            content = content.Substring(3, content.Length - 3);

            // content = sbTemp.ToString();
        }
        content = StrValue;
        return content;
    }

Hi,
Please refer to my post on
http://www.daniweb.com/forums/post1256376.html

Dim dv As New DataView()
With dv
	.Table = ds.Tables(0)
	.RowFilter = "dataColumnName='A%'"
End With
yourGridView.DataSource = dv
yourGridView.DataBind()

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.