Simple way to Show/Hide Filter in RadGrid


Description:

Telerik’s RadGrid provides in-built search functionality. You can find the search functionality at top of the grid just below the header columns. But user should be able to hide the same to save unnecessary space all the time from the grid.

RadGrid Example when Filter is not visible to the user:

Radgrid without Filter

Radgrid without Filter

RadGrid Example when Filter is visible to the user:

RadGrid With Filter

RadGrid With Filter


Code:

First of all in RadGrid’s MasterTableView tag, add following checkbox:

<MasterTableView Width=”100%” CommandItemDisplay=”Top” GroupLoadMode=”Server”>

<CommandItemTemplate>

<asp:CheckBox ID=”CheckBox2″ AutoPostBack=”true” CssClass=”CheckBox” runat=”server”

ForeColor=”White” Text=”Show Filter” />

</CommandItemTemplate>

After that add the ItemCreated event in the RadGrid as following code does:

protected void grdManageTransType_ItemCreated(object sender, GridItemEventArgs e)

{

if (e.Item.ItemType == GridItemType.CommandItem)

{

GridCommandItem commandItem = e.Item as GridCommandItem;

CheckBox chkFilter2 = (CheckBox)commandItem.FindControl(“CheckBox2”);

chkFilter2.AutoPostBack = true;

chkFilter2.ForeColor = System.Drawing.Color.White;

chkFilter2.Checked = grdManageTransType.AllowFilteringByColumn;

//Adding the event handler

chkFilter2.CheckedChanged += new EventHandler(chkFilter2_CheckedChanged);

}

if (e.Item.ItemType == GridItemType.SelectedItem)

e.Item.BackColor = Color.FromName(“#FFEDC1”);

}

In above portion of code we are creating CheckedChanged event of the checkbox. So now create the CheckedChanged event of the checkbox as per following code:

protected void chkFilter2_CheckedChanged(object sender, EventArgs e)

{

grdManageTransType.AllowFilteringByColumn = !grdManageTransType.AllowFilteringByColumn;

grdManageTransType.Rebind();

}

As per the above code portions, we can the checkbox at the top of the RadGrid. On click of that checkbox RadGrid’s filter should be visible on or off.

Count items in Telerik’s RadComboBox


Description:

It is general requirement to show and count number of items in Telerik’s RadComboBox. And we can do that by using a simple javascript function.

Code:

In javasript:

function UpdateItemCountField(sender, args) {

//set the footer text

sender.get_dropDownElement().lastChild.innerHTML = “A total of “ + sender.get_items().get_count() + ” items”;

}

In aspx page:

add footer template as shown below code.

<FooterTemplate>

A total of

<asp:Literal runat=”server” ID=”RadComboItemsCount” />

items

</FooterTemplate>

Compare two data tables in c#


Description:

Comparing two datatable and get the final table with difference in it, this kind of requirement is generally arise generally when we are doing multiple insert or update. I have scenario like get the two different tables data, compare it and if there is difference in output then insert that data into the third table. So use the approach to get the data from database into different table and then insert the difference output using SqlBulkCopy in to the third table.

 

Code:

Following is the code to compare two datatable. Just copy and paste this class into your code and its working.

 

using System;

using System.Data;

 

namespace [Namespace Name]

{

public class CompareDataTables

{

public static DataTable CompareTables(DataTable first, DataTable second)

{

first.TableName = “FirstTable”;

second.TableName = “SecondTable”;

 

//Create Empty Table

DataTable table = new DataTable(“Difference”);

 

try

{

//Must use a Dataset to make use of a DataRelation object

using (DataSet ds = new DataSet())

{

//Add tables

ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });

 

//Get Columns for DataRelation

//DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];

DataColumn[] firstcolumns = new DataColumn[2];

firstcolumns[0] = ds.Tables[0].Columns[1];

firstcolumns[1] = ds.Tables[0].Columns[7];

 

//for (int i = 0; i < firstcolumns.Length; i++)

//{

// firstcolumns[i] = ds.Tables[0].Columns[i];

//}

 

//DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];

DataColumn[] secondcolumns = new DataColumn[2];

secondcolumns[0] = ds.Tables[1].Columns[1];

secondcolumns[1] = ds.Tables[1].Columns[7];

 

//for (int i = 0; i < secondcolumns.Length; i++)

//{

// secondcolumns[i] = ds.Tables[1].Columns[i];

//}

 

//Create DataRelation

DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);

 

ds.Relations.Add(r);

 

//Create columns for return table

for (int i = 0; i < first.Columns.Count; i++)

{

table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);

}

 

//If First Row not in Second, Add to return table.

table.BeginLoadData();

 

foreach (DataRow parentrow in ds.Tables[0].Rows)

{

DataRow[] childrows = parentrow.GetChildRows(r);

if (childrows == null || childrows.Length == 0)

table.LoadDataRow(parentrow.ItemArray, true);

}

 

table.EndLoadData();

 

}

}

catch (Exception ex)

{

throw ex;

}

 

return table;

}

}

}

 

Code Explanation:

 

  1. CompareTables method is static, so you can call it directly from your class. Just add the appropriate reference.
  2. Pass the two data tables in this method.
  3. Then creates the dataset.
  4. Add two tables into the dataset.
  5. Then important part, define the columns two compare the data table. In my case i just want to check the column 1 & 7. If there is a mismatch between this two then inserts the record. You can modify the code as per your requirement or just uncomment the line above and below my code , which adds all columns.
  6. Creates data relation on that column(s).
  7. Add relation two the dataset.
  8. Create the target or difference table colums
  9. Then load the difference data in it and return the difference table.