winForm Distinct Table
winForm Distinct Table
//Following function will return Distinct records
public DataTable GetDistinctRecords(DataTable dt, string[] Columns)
{
try
{
DataTable dtUniqRecords = new DataTable();
dtUniqRecords = dt.DefaultView.ToTable(true, Columns);
return dtUniqRecords;
}
catch (Exception ex)
{
throw ex;
}
}
//For example
DataTable dt_datasource = new DataTable();
dt_datasource= "Source where come from... => use SqlConnection, SqlCommand ....";
string[] TobeDistinct = { "result", "mainlabel", "UPCSticker", "Foldingmethod", "Remark" };
DataTable queryDistinctTable = new DataTable();
queryDistinctTable = ds_querydata.Tables[0].DefaultView.ToTable(true, TobeDistinct);
if (queryDistinctTable.Rows.Count > 1)
{
MessageBox.Show("Can not modify the Data ,because those are different result.");
toolBarSave.Enabled = false;
return;
}
else
{
Radio_possible.EditValue = queryDistinctTable.Rows[0]["result"].ToString();
check_mainlabel.EditValue = queryDistinctTable.Rows[0]["mainlabel"].ToString();
check_UPCSticker.EditValue =queryDistinctTable.[0].Rows[0]["UPCSticker"].ToString();
check_Foldingmethod.EditValue = queryDistinctTable.Rows[0]["Foldingmethod"].ToString();
txt_remark.Text = queryDistinctTable.Rows[0]["Remark"].ToString();
}