0

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.

5
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by lm111
0

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.

0

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!!!

0

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

0

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;
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.