Hi, I have been using asp.net for a short period of time now and really enjoy the functionality offered by visual studio. However I see there is no automated way of generating a search of a datagrid. What i want to do is simple take the string from a textbot when the search button is pressed and either highlight the matches in the datagrid or to regenerate the datagrid displaying the matches. I want to search only on column which will be the resource name. I have looked throug many threads and there have been a few solutions but they all seem to be in VB.NET. As I have only ever used C# I am terrified by this code! If anyone would be so kind to either link me to a page with C# code for searching a datagrid or to perhaps post some code of their own it would be greatly appreciated.
Thanks
.NetIdiot

add below code in aspx.cs

public partial class _Default : System.Web.UI.Page
{
    DataGrid dg = new DataGrid();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        dg.DataSource = SqlDataSource1;
        dg.DataBind();
        Panel1.Controls.Add(dg);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (DataGridItem dgi in dg.Items)
        {
            if (dgi.Cells[0].Text == TextBox1.Text)
            {
                for(int i=0;i<dgi.Cells.Count;i++)
                dgi.Cells[i].BackColor = System.Drawing.Color.Beige;
            }
        }
    }
}

add below code in aspx.cs

public partial class _Default : System.Web.UI.Page
{
    DataGrid dg = new DataGrid();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        dg.DataSource = SqlDataSource1;
        dg.DataBind();
        Panel1.Controls.Add(dg);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (DataGridItem dgi in dg.Items)
        {
            if (dgi.Cells[0].Text == TextBox1.Text)
            {
                for(int i=0;i<dgi.Cells.Count;i++)
                dgi.Cells[i].BackColor = System.Drawing.Color.Beige;
            }
        }
    }
}

Thanks, when i paste code however it says Paenl1 does not exist in current context. Could you explain what panel1 is ?
thanks!

Nevermind Ive added a Panel from the toolbar and placed all contents of the page inside the panel. Is this correct?

Ok i have disabled callbacks and selecting but now when i launch the page it simply display two copys of my datagrid and the search appears to do nothing.

In the code I pasted above, the DataGrid was added dynamically, and therefore need to be put either on a Panel or directly on the page.

DataGrid dg = new DataGrid();
    protected void Page_Load(object sender, EventArgs e)
    { 
        dg.DataSource = SqlDataSource1;
        dg.DataBind();
//Here you add to Panel1
        [B]Panel1.Controls.Add(dg);[/B]
//Or You can directly add to page as below
       [B] //Page.Controls.Add(dg); [/B]
    }

You just need to put a Panel on the page. And dynamically add the DataGrid on runtime.
Therefore you need not add any controls Manually on to the Panel.

Thanks,
Chithra

Try to search on datasource.

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.