You can remove the duplicated values in
RowDataBoundEvent in GridView
Sample Code:
protected void GridView6_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//switch for first row
if (e.Row.RowIndex == 1)
{
GridViewRow Gprev = GridView6.Rows[e.Row.RowIndex - 1];
//Ensure that you have changed the column index according to your column
if (Gprev.Cells[0].Text.Equals(e.Row.Cells[0].Text))
{
e.Row.Cells[0].Text = "";
}
}
//now continue with the rest of the rows
if (e.Row.RowIndex > 1)
{
//set reference to the row index and the current value
int intC = e.Row.RowIndex;
string lookfor = e.Row.Cells[0].Text;
//now loop back through checking previous entries for matches
do
{
GridViewRow Gprev = GridView6.Rows[intC - 1];
if (Gprev.Cells[0].Text.Equals(e.Row.Cells[0].Text))
{
e.Row.Cells[0].Text = "";
}
intC = intC - 1;
} while (intC > 0);
}
}
}
Complete Code:
HTML
<asp:GridView ID="GridView6" runat="server" AutoGenerateColumns="False" DataKeyNames="Fruits"
ForeColor="#333333" GridLines="None"
onrowdatabound="GridView6_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="Fruits" DataField="Fruits" />
</Columns>
</asp:GridView>
C#:
/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Assign the data to GridView
GridView6.DataSource = this.Get_Fruits();
//Bind the Grid
GridView6.DataBind();
}
}
//Populate some dummy data
public DataTable Get_Fruits()
{
DataTable dt = new DataTable();
DataRow dr;
string Item = "Orange,Banana,Orange,Apple,Orange,Apple,Chikku";
string[] list2 = Item.Split(',');
dt.Columns.Add("Fruits", typeof(string));
for (int i = 0; i < list2.Length; i++)
{
dr = dt.NewRow();
dr["Fruits"] = list2[i];
dt.Rows.Add(dr);
}
return dt;
}
/// <summary>
/// Handles the RowDataBound event of the GridView6 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="GridViewRowEventArgs"/> instance containing the event data.</param>
protected void GridView6_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//switch for first row
if (e.Row.RowIndex == 1)
{
GridViewRow Gprev = GridView6.Rows[e.Row.RowIndex - 1];
//Ensure that you have changed the column index according to your column
if (Gprev.Cells[0].Text.Equals(e.Row.Cells[0].Text))
{
e.Row.Cells[0].Text = "";
}
}
//now continue with the rest of the rows
if (e.Row.RowIndex > 1)
{
//set reference to the row index and the current value
int intC = e.Row.RowIndex;
string lookfor = e.Row.Cells[0].Text;
//now loop back through checking previous entries for matches
do
{
GridViewRow Gprev = GridView6.Rows[intC - 1];
if (Gprev.Cells[0].Text.Equals(e.Row.Cells[0].Text))
{
e.Row.Cells[0].Text = "";
}
intC = intC - 1;
} while (intC > 0);
}
}
}
Refer the below links for more details
http://www.tek-tips.com/faqs.cfm?fid=6665
http://csharpdotnetfreak.blogspot.com/2011/12/remove-duplicate-rows-datatable.html Thanks,
A2H
My Blog
Venky.Net, if this helps please login to Mark As Answer. | Alert Moderator