This is a migrated thread and some comments may be shown as answers.

[Solved] How to bind a SearchResult correct on a Grid?

2 Answers 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Uwe
Top achievements
Rank 1
Uwe asked on 15 Feb 2013, 03:34 PM
Hello,

I've tried to use this example Simple Databinding in a project, but I have run in to a  problem because my case differs a bit from the example code..

On the Website i have 3 Textfileds (Searchfield, Searchcriterium,SearchValue), a SerchButton and a ReloadButton.
When the User collect his Search and press the SearchButton , i generate the SQL-String and get the Data with a SQLDataSource.

My problems are when the Site starts at first witout a search, i get exceptions ( no search = no data)  and witch Method is correct
and how i do this to bind and  get a result with the RadGrid? Its my first project with the ASP.NET and the RadControls.

Best Regards
Uwe


2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 18 Feb 2013, 04:27 AM
Hi,

Please take a look into the sample I have created with your requirement.

ASPX:
<asp:TextBox ID="Searchfield" runat="server"></asp:TextBox>
<asp:Button ID="SearchButton" runat="server" OnClick="SearchButton_Click" />
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
    Visible="false">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="EmployeeID">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
public static string connection = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString3"].ConnectionString;
SqlConnection conn = new SqlConnection(connection);
public SqlCommand SqlCommand = new SqlCommand();
DataTable dt1 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
 
}
static string Search = string.Empty;
protected void SearchButton_Click(object sender, EventArgs e)
{
    Search = Searchfield.Text;
    RadGrid1.Visible = true;
    RadGrid1.Rebind();
}
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    string selectQuery1 = "SELECT TOP 10 *  FROM Orders where OrderID='" + Search + "'";
    SqlDataAdapter adapter1 = new SqlDataAdapter(selectQuery1, conn);
    conn.Open();
    adapter1.Fill(dt1);
    conn.Close();
    RadGrid1.DataSource = dt1;     
}

Thanks,
Shinu.
0
Uwe
Top achievements
Rank 1
answered on 18 Feb 2013, 09:01 PM
Hello Shinu,

thanks a lot for your help.
It works very fine and i have also learn a lot.

Best Regards
Uwe
Tags
Grid
Asked by
Uwe
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Uwe
Top achievements
Rank 1
Share this question
or