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

Dynamic Grid Creation - Sorting Causes Problems

9 Answers 92 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 21 Sep 2012, 01:27 PM
All

Here is my scenario...
I have a combo box control with some values in it. Based on the selected value, I run a stored procedure to return a table of data. Each of the items in the combo box will call a different stored procedure, which will return a totally different result set. I need to do everything dynamically without hard coding any column names. 

I can get this all to work. My issue comes when I do the sorting on the grid. With each sort, it initiates a postback thus resetting to the default value of the grid and not sorting the correct grid/datatable that was most recently populated. I have seen some topics similar around the forum, but nothing that I found helpful. 

Hope you guys can help!
- JB

9 Answers, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 21 Sep 2012, 02:41 PM
Hi Josh,

Did you mean it's resetting the value of the ComboBox on postback in this statement:  "With each sort, it initiates a postback thus resetting to the default value of the grid and not sorting the correct grid/datatable that was most recently populated. " ?

Is the ComboBox inside or outside of the RadGrid? How are you binding the ComboBox to it's datasource? Are you using the RadGrid's NeedDataSource event to handle assigning the correct datasource to the Radgrid? 

Sorry for so many questions. 

Casey
0
Josh
Top achievements
Rank 1
answered on 21 Sep 2012, 03:00 PM
Yea, it does actually seem like it's resetting the selected value of the Combobox in code although on screen it does not change. The combobox is outside of the grid, and for now, I'm simply hardcoding a few values, no datasource on the combo. 
Currently, I'm not using the NeedDataSource event. Here's my code...

aspx code
<telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="true">
    <Items>
        <telerik:RadComboBoxItem runat="server" Text="sp_ReadStopData"
            Value="sp_ReadStopData" />
        <telerik:RadComboBoxItem runat="server" Text="sp_ReadFuelSurchargeData"
            Value="sp_ReadFuelSurchargeData" />
    </Items>
</telerik:RadComboBox>
 
 
    <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
    </telerik:RadScriptManager>
 
    <div style="width: 92%">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        </telerik:RadAjaxManager>
    </div>

vb code behind...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim grid As RadGrid = CType(PlaceHolder1.FindControl("RadGrid1"), RadGrid)
    RadAjaxManager1.AjaxSettings.AddAjaxSetting(grid, grid)
End Sub
 
Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Init
    PopulateGridOnPageInit(RadComboBox1.SelectedValue)
End Sub
 
Protected Sub PopulateGridOnPageInit(ByVal mySP As String)
    If Not RadComboBox1.SelectedValue = "" Then
        Dim RadGrid1 As RadGrid = New RadGrid
 
        RadGrid1.ID = "RadGrid1"
 
        RadGrid1.Width = Unit.Percentage(100)
        RadGrid1.PageSize = 5
        RadGrid1.AllowPaging = True
        RadGrid1.AllowSorting = True
        RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
        RadGrid1.AutoGenerateColumns = False
        RadGrid1.Skin = "MetroTouch"
        RadGrid1.BorderStyle = BorderStyle.None
        RadGrid1.MasterTableView.EnableColumnsViewState = False
        RadGrid1.MasterTableView.PageSize = 15
 
        Dim tblStopData As DataTable = New DataTable
 
        Dim ConnString As String = ConfigurationManager.ConnectionStrings("SPsqlConnect").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(ConnString)
 
        Dim cmd As SqlCommand = New SqlCommand(mySP, conn)
 
        cmd.CommandType = CommandType.StoredProcedure
 
        Dim adapter As New SqlDataAdapter(cmd)
        adapter.SelectCommand.CommandTimeout = 300
 
        adapter.Fill(tblStopData)
 
        Dim name(tblStopData.Columns.Count) As String
        Dim i As Integer = 0
        For Each column As DataColumn In tblStopData.Columns
            Dim boundColumn As GridBoundColumn = New GridBoundColumn
            RadGrid1.MasterTableView.Columns.Add(boundColumn)
            boundColumn.DataField = column.ColumnName
            boundColumn.HeaderText = column.ColumnName
        Next
 
        RadGrid1.DataSource = tblStopData
 
        conn.Close()
 
        Me.PlaceHolder1.Controls.Add(RadGrid1)
    Else
        Dim RadGrid1 As RadGrid = New RadGrid
 
        RadGrid1.ID = "RadGrid1"
        Me.PlaceHolder1.Controls.Add(RadGrid1)
    End If
 
End Sub
 
Protected Sub RadComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles RadComboBox1.SelectedIndexChanged
    Dim oldGrid As RadGrid
    oldGrid = PlaceHolder1.FindControl("RadGrid1")
    PlaceHolder1.Controls.Remove(oldGrid)
 
    PopulateGridOnPageInit(RadComboBox1.SelectedValue)
End Sub
0
Accepted
Casey
Top achievements
Rank 1
answered on 21 Sep 2012, 03:15 PM
Josh,

I think it may have something to do with trying to use the RadComboBox's SelectedValue in the Page_Init event. Could you try moving the call to "PopulateGridOnPageInit" method to your Page_Load event? 

To determine if the value isn't set at the time of Page_Init, you could check the value of RadComboBox1.SelectedValue in the Page_Init and Page_Load events while debugging.

I hope this helps!
Casey
0
Josh
Top achievements
Rank 1
answered on 21 Sep 2012, 03:18 PM
Awesome, totally works now! Such a simple change goes a long way! Thanks a lot for your help and quick response.
0
Casey
Top achievements
Rank 1
answered on 21 Sep 2012, 03:20 PM
No problem. Glad I was able to help!
0
Alex
Top achievements
Rank 1
answered on 02 Apr 2013, 09:08 PM
I think this is similar to my problem. I have 3 controls and a RadGrid. My 3 controls are a RadTextBox1, RadComboBox1 and RadComboBox2. If a user enters text into the textbox and filters the RadGrid I want to update the datasource of my RadComboBoxes to reflect these changes.

For instance, if the original unfiltered grid contains 100 rows and there are 50 different employees assigned to one-or-another records. Before any filtering is done the RadComboBox (used for employee names) contains 50 entries.  If the user filters on the textbox for a name and 20 records are returned and of those 20 records there are only 5 employee names, I want my RadComboBox (which contains employee names) to now reflect only these 5 names.

How is this accomplished? I am banging my head ...
0
Casey
Top achievements
Rank 1
answered on 03 Apr 2013, 12:56 PM
Hi Alex,

How are you initially setting the datasource of the RadComboBox to be only the 50 distinct employees? In the same event where you are filtering the RadGrid, you should be able to change the datasource of the RadComboBox. If that does not work, you may need to post the relevant code so I can see how you are doing it. 

I hope this helps!

Thanks,
Casey
0
Alex
Top achievements
Rank 1
answered on 03 Apr 2013, 02:49 PM
I have a separate DataTable for each RadComboBox control and one for the grid. My names combobox DataTable is called "GetAttyData", my clientID combobox is called "GetNumberData", and my RadGrid DataTable is called "GetData". (I am real original with my names) :)

Here are my 4 different data queries:
private DataTable GetData()
        {
            String ConnString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand("SELECT TOP 1000 [CustName], [Atty], [ClientNum] FROM [dummytable]", conn);
            DataTable myDataTable = new DataTable();
            DataRow[] rows;
 
            try
            {
                conn.Open();
                try
                {
                    adapter.Fill(myDataTable);
                }
                finally
                {
                    conn.Close();
                }
 
                RadGrid1.DataSource = myDataTable;
                GetAttyData();
                RadComboBox1.DataBind();
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('GetData is broken');", true);
            }
             
            //if (myDataTable.Rows.Count != 0)
            //{
            if (!IsPostBack)
            {
                 
                return myDataTable;
            }
            else
            {
                //if (RadComboBox1.SelectedIndex != 0)
                //{
                //    Label1.Text = RadComboBox1.Text;
                //    rows = myDataTable.Select("[Billing_Atty] LIKE '%" + RadComboBox1.Text + "%'");
                //}
                 
                //if (RadComboBox2.SelectedIndex != 0)
                //{
                //    Label2.Text = RadComboBox2.Text;
                //    rows = myDataTable.Select("[ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
                //}
                 
                //if ((!String.IsNullOrEmpty(RadComboBox1.Text) && (!String.IsNullOrEmpty(RadComboBox2.Text))))
 
                if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex == 0) && (RadComboBox2.SelectedIndex == 0))
                {
                    rows = myDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%'");
                }
                else if ((RadTextBox1.Text == "") && (RadComboBox1.SelectedIndex != 0) && (RadComboBox2.SelectedIndex == 0))
                {
                    Label1.Text = RadComboBox1.Text;
                    rows = myDataTable.Select("[Atty] LIKE '%" + RadComboBox1.Text + "%'");
                }
                else if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex != 0) && (RadComboBox2.SelectedIndex == 0))
                {
                    Label1.Text = RadComboBox1.Text;
                    rows = myDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%' AND [Atty] LIKE '%" + RadComboBox1.Text + "%'");
                }
                else if ((RadTextBox1.Text == "") && (RadComboBox1.SelectedIndex == 0) && (RadComboBox2.SelectedIndex != 0))
                {
                    Label2.Text = RadComboBox2.Text;
                    rows = myDataTable.Select("[ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
                }
                else if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex == 0) && (RadComboBox2.SelectedIndex != 0))
                {
                    Label2.Text = RadComboBox2.Text;
                    rows = myDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%' AND [ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
                }
                else if ((RadTextBox1.Text == "") && (RadComboBox1.SelectedIndex != 0) && (RadComboBox2.SelectedIndex != 0))
                {
                    Label2.Text = RadComboBox2.Text;
                    rows = myDataTable.Select("[Atty] LIKE '%" + RadComboBox1.Text + "%' AND [ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
                }
                else if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex != 0) && (RadComboBox2.SelectedIndex != 0))
                {
                    rows = myDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%' AND [Atty] LIKE '%" + RadComboBox1.Text + "%' AND [ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
                }
                else
                {
                    return new DataTable();
                }
                 
                if (myDataTable.Rows.Count == 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "norowalert", "alert('I cannot complete your request.\nThere are no records that meet your criteria.');", true);
                    return new DataTable();
                }
                if (rows.Length == 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "norowalert", "alert('I cannot complete your request.\nThere are no records that meet your criteria.');", true);
                    return new DataTable();
                     
                }
                //else
                //{
                //    return rows.CopyToDataTable();
                //    RadGrid1.DataBind();
 
                //}
                if (rows.Length >= 1)
                {
                    return rows.CopyToDataTable();
                    RadGrid1.DataBind();
                }
                 
                return rows.CopyToDataTable();
                //RefreshControls();
                //return myDataTable;
            }
        }
 
        private DataTable GetClearData()
        {
            String ConnString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand("SELECT TOP 1000 [CustName], [Atty], [ClientNum] FROM [dummytable]", conn);
            DataTable myDataTable = new DataTable();
 
            try
            {
                conn.Open();
                try
                {
                    adapter.Fill(myDataTable);
                }
                finally
                {
                    conn.Close();
                }
 
                RadGrid1.DataSource = myDataTable;
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('GetClearData is broken');", true);
            }
 
            return myDataTable;       
        }
     
    private DataTable GetAttyData()
        {
            String ConnString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand("SELECT DISTINCT TOP 1000 [Atty],[ClientNum] FROM [dummytable]", conn);
            DataTable myAttyDataTable = new DataTable();
            DataRow[] rows;
 
            try
            {
                conn.Open();
                try
                {
                    adapter.Fill(myAttyDataTable);
                }
                finally
                {
                    conn.Close();
                }
 
                RadComboBox1.DataSource = myAttyDataTable;
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('GetAttyData is broken');", true);
            }
 
            if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex == 0))
            {
                rows = myAttyDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%'");
            }
            else if ((RadTextBox1.Text == "") && (RadComboBox2.SelectedIndex != 0))
            {
                rows = myAttyDataTable.Select("[ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
            }
            else if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex != 0))
            {
                rows = myAttyDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%' AND [ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
            }
            else
            {
                return new DataTable();
            }
            //return myDataTable;
            return rows.CopyToDataTable();
        }
 
    private DataTable GetNumberData()
    {
        String ConnString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand("SELECT DISTINCT [ClientNum] FROM [dummytable] ORDER BY [Client_Num]", conn);
         
        DataTable myDataTable = new DataTable();
 
        try
        {
            conn.Open();
            try
            {
                adapter.Fill(myDataTable);
            }
            finally
            {
                conn.Close();
            }
 
            RadComboBox2.DataSource = myDataTable;
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Client Number Failure');", true);
        }
 
        return myDataTable;
    }

Here is the remainder of my code-behind:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadComboBox1.DataSource = GetAttyData();
                RadComboBox1.DataBind();
                RadComboBox2.DataSource = GetNumberData();
                RadComboBox2.DataBind();
                RadGrid1.DataSource = GetData();
                RadGrid1.DataBind();
            }
        }
 
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedIndexes.Count == 0)
                RadGrid1.SelectedIndexes.Add(0);
        }
 
        protected void Page_Init(object sender, System.EventArgs e)
        {
            RadComboBox1.TextChanged += new EventHandler(RadComboBox1_TextChanged);
            RadComboBox2.TextChanged += new EventHandler(RadComboBox2_TextChanged);
            RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
        }
 
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = GetData();
        }
                 
        private void RefreshControls()
        {
            RadGrid1.Rebind();
             
        }
 
        void RadComboBox1_TextChanged(object sender, EventArgs e)
        {

            RadGrid1.DataSource = GetData();
            RadGrid1.DataBind();
        }
 
        void RadComboBox2_TextChanged(object sender, EventArgs e)
        {          
            RadGrid1.DataSource = GetData();           
            RadGrid1.DataBind();
        }
 
        protected void RadComboBox1_DataBound(object sender, EventArgs e)
        {
            var combo = (RadComboBox)sender;
            combo.Items.Insert(0, new RadComboBoxItem("Select an Attorney", string.Empty));
        }
 
        protected void RadComboBox2_DataBound(object sender, EventArgs e)
        {
            var combo = (RadComboBox)sender;
            combo.Items.Insert(0, new RadComboBoxItem("Select a Client Number", string.Empty));
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            RadComboBox1.SelectedIndex = 0;
            RadComboBox2.SelectedIndex = 0;
            RadGrid1.DataSource = GetClearData();
            RadGrid1.DataBind();
 
        }
 
        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                RadGrid1.DataSource = GetData();
                RadGrid1.DataBind();
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Button2 is broken');", true);
            }
            finally
            {
                 
            }
             
        }


Here is my ASPX page:
<form id="form1" runat="server">
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"> </script>
        <script type="text/javascript">
            function nofilter() { $("#RadComboBox1").prepend("<option value='' selected='selected'>No Filter</option>"); }
        </script>
    </telerik:RadCodeBlock>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <telerik:RadTextBox ID="RadTextBox1" runat="server">
        </telerik:RadTextBox>
        <telerik:RadComboBox ID="RadComboBox1" runat="server" DataTextField="Atty" DataValueField="Atty" AutoPostBack="false" OnDataBound="RadComboBox1_DataBound">
        </telerik:RadComboBox>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <telerik:RadComboBox ID="RadComboBox2"
            runat="server" DataTextField="ClientNum" DataValueField="ClientNum" AutoPostBack="false" OnDataBound="RadComboBox2_DataBound">
        </telerik:RadComboBox>
        <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        <asp:Button ID="Button1"
            runat="server" Text="Reset Filter" onclick="Button1_Click" />
            <asp:Button ID="Button2"
                runat="server" Text="Filter Me" onclick="Button2_Click" />
        <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" GridLines="None"
            OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="True">
        </telerik:RadGrid>
    </div>
    </form>
0
Casey
Top achievements
Rank 1
answered on 03 Apr 2013, 08:56 PM
The first thing I noticed was that you are using RadGrid1.DataBind() along with the NeedDataSource event. Telerik recommends against doing so. I always use .Rebind() - which triggers the NeedDataSource event. 

Advanced Data-binding

I tried moving the code around, and instead of setting the combobox datasource inside of the methods, I set the datasource equal to the datatable returned by the methods. I tried to get rid of all places where RadGrid1.DataBind() were called, and I modified the if condition in the GetAttyData() method, as it was checking the selected index of RadComboBox1 instead of RadComboBox2.

I hope this helps!

Casey
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        RadComboBox1.DataSource = GetAttyData();
        RadComboBox1.DataBind();
        RadComboBox2.DataSource = GetNumberData();
        RadComboBox2.DataBind();
        RadGrid1.DataSource = null;
        RadGrid1.Rebind();
    }
}
 
protected void Page_PreRender(object sender, EventArgs e)
{
    if (RadGrid1.SelectedIndexes.Count == 0)
        RadGrid1.SelectedIndexes.Add(0);
}
 
protected void Page_Init(object sender, System.EventArgs e)
{
    RadComboBox1.TextChanged += new EventHandler(RadComboBox1_TextChanged);
    RadComboBox2.TextChanged += new EventHandler(RadComboBox2_TextChanged);
    RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
}
 
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = GetData();
}
 
private void RefreshControls()
{
    RadGrid1.DataSource = null;
    RadGrid1.Rebind();
}
 
void RadComboBox1_TextChanged(object sender, EventArgs e)
{
    RadGrid1.DataSource = null;
    RadGrid1.Rebind();
}
 
void RadComboBox2_TextChanged(object sender, EventArgs e)
{
    RadGrid1.DataSource = null;
    RadGrid1.Rebind();
}
 
protected void RadComboBox1_DataBound(object sender, EventArgs e)
{
    var combo = (RadComboBox)sender;
    combo.Items.Insert(0, new RadComboBoxItem("Select an Attorney", string.Empty));
}
 
protected void RadComboBox2_DataBound(object sender, EventArgs e)
{
    var combo = (RadComboBox)sender;
    combo.Items.Insert(0, new RadComboBoxItem("Select a Client Number", string.Empty));
}
 
protected void Button1_Click(object sender, EventArgs e)
{
    RadTextBox1.Text = "";
    RadComboBox1.Items.Clear();
    RadComboBox2.Items.Clear();
    RadComboBox1.DataSource = GetAttyData();
    RadComboBox1.DataBind();
    RadComboBox1.SelectedIndex = 0;
    RadComboBox2.DataSource = GetNumberData();
    RadComboBox2.DataBind();
    RadComboBox2.SelectedIndex = 0;
    RadGrid1.DataSource = null;
    RadGrid1.Rebind();
 
}
 
protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
        bool bindCombo1 = false;
        bool bindCombo2 = false;
        if (RadComboBox1.SelectedItem == null)
        {
            bindCombo1 = true;
        }
        else if (RadComboBox1.SelectedIndex == 0)
        {
            bindCombo1 = true;
        }
        if (RadComboBox2.SelectedItem == null)
        {
            bindCombo2 = true;
        }
        else if (RadComboBox2.SelectedIndex == 0)
        {
            bindCombo2 = true;
        }
 
        if (bindCombo1)
        {
            RadComboBox1.Items.Clear();
            RadComboBox1.DataSource = GetAttyData();
            RadComboBox1.DataBind();
            RadComboBox1.SelectedIndex = 0;
        }
        if (bindCombo2)
        {
            RadComboBox2.Items.Clear();
            RadComboBox2.DataSource = GetAttyData();
            RadComboBox2.DataBind();
            RadComboBox2.SelectedIndex = 0;
        }
        RadGrid1.DataSource = null;
        RadGrid1.Rebind();
    }
    catch
    {
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Button2 is broken');", true);
    }
    finally
    {
 
    }
 
}
 
private DataTable GetData()
{
    String ConnString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand("SELECT TOP 1000 [CustName], [Atty], [ClientNum] FROM [dummytable]", conn);
    DataTable myDataTable = new DataTable();
    DataRow[] rows;
 
    try
    {
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
    }
    catch
    {
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('GetData is broken');", true);
    }
 
    if (!IsPostBack)
    {
 
        return myDataTable;
    }
    else
    {
 
        if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex == 0) && (RadComboBox2.SelectedIndex == 0))
        {
            rows = myDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%'");
        }
        else if ((RadTextBox1.Text == "") && (RadComboBox1.SelectedIndex != 0) && (RadComboBox2.SelectedIndex == 0))
        {
            Label1.Text = RadComboBox1.Text;
            rows = myDataTable.Select("[Atty] LIKE '%" + RadComboBox1.Text + "%'");
        }
        else if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex != 0) && (RadComboBox2.SelectedIndex == 0))
        {
            Label1.Text = RadComboBox1.Text;
            rows = myDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%' AND [Atty] LIKE '%" + RadComboBox1.Text + "%'");
        }
        else if ((RadTextBox1.Text == "") && (RadComboBox1.SelectedIndex == 0) && (RadComboBox2.SelectedIndex != 0))
        {
            Label2.Text = RadComboBox2.Text;
            rows = myDataTable.Select("[ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
        }
        else if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex == 0) && (RadComboBox2.SelectedIndex != 0))
        {
            Label2.Text = RadComboBox2.Text;
            rows = myDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%' AND [ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
        }
        else if ((RadTextBox1.Text == "") && (RadComboBox1.SelectedIndex != 0) && (RadComboBox2.SelectedIndex != 0))
        {
            Label2.Text = RadComboBox2.Text;
            rows = myDataTable.Select("[Atty] LIKE '%" + RadComboBox1.Text + "%' AND [ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
        }
        else if ((RadTextBox1.Text != "") && (RadComboBox1.SelectedIndex != 0) && (RadComboBox2.SelectedIndex != 0))
        {
            rows = myDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%' AND [Atty] LIKE '%" + RadComboBox1.Text + "%' AND [ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
        }
        else
        {
            return myDataTable;
        }
 
        if (myDataTable.Rows.Count == 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "norowalert", "alert('I cannot complete your request.\nThere are no records that meet your criteria.');", true);
            return new DataTable();
        }
        else if (rows.Length == 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "norowalert", "alert('I cannot complete your request.\nThere are no records that meet your criteria.');", true);
            return new DataTable();
 
        }
        else
        {
            return rows.CopyToDataTable();
        }
    }
}
 
private DataTable GetAttyData()
{
    String ConnString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand("SELECT DISTINCT TOP 1000 [Atty],[ClientNum] FROM [dummytable]", conn);
    DataTable myAttyDataTable = new DataTable();
    DataRow[] rows;
 
    try
    {
        conn.Open();
        try
        {
            adapter.Fill(myAttyDataTable);
        }
        finally
        {
            conn.Close();
        }
    }
    catch
    {
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('GetAttyData is broken');", true);
    }
 
    if ((RadTextBox1.Text != "") && (RadComboBox2.SelectedIndex == 0))
    {
        rows = myAttyDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%'");
    }
    else if ((RadTextBox1.Text == "") && (RadComboBox2.SelectedIndex != 0))
    {
        rows = myAttyDataTable.Select("[ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
    }
    else if ((RadTextBox1.Text != "") && (RadComboBox2.SelectedIndex != 0))
    {
        rows = myAttyDataTable.Select("[CustName] LIKE '%" + RadTextBox1.Text + "%' AND [ClientNum] LIKE '%" + RadComboBox2.Text + "%'");
    }
    else
    {
        return myAttyDataTable;
    }
    return rows.CopyToDataTable();
}
 
private DataTable GetNumberData()
{
    String ConnString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand("SELECT DISTINCT [ClientNum] FROM [dummytable] ORDER BY [Client_Num]", conn);
 
    DataTable myDataTable = new DataTable();
 
    try
    {
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
    }
    catch
    {
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Client Number Failure');", true);
    }
 
    return myDataTable;
}
Tags
Grid
Asked by
Josh
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
Josh
Top achievements
Rank 1
Alex
Top achievements
Rank 1
Share this question
or