0

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

3
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by __avd
0

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;
            }
        }
    }
}
0

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!

0

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

0

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.

0

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

This topic has been dead for over six months. 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.