|
|
|
[C#] ช่วยหน่อยคร๊ จะทำการลบข้อมูลที่ซ้ำกันออกจาก datatable |
|
|
|
|
|
|
|
ก็ Select Distinct ตั้งแต่ใน Query เลยครับ ถ้า ไม่ใช้ ทุก Column ไม่แน่นำให้ใช้ * ในการ Query
Code (SQL)
SELECT DISTINCT Col1,Col2 FROM myTable
|
|
|
|
|
Date :
2016-06-10 16:35:18 |
By :
taotechnocom |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
private void Form1_Load(object sender, EventArgs e)
{
//defining the number of Column
dataGridView1.ColumnCount = 3;
//Adding Columns Name
dataGridView1.Columns[0].Name = "Product ID";
dataGridView1.Columns[1].Name = "Prduct Name";
dataGridView1.Columns[2].Name = "Product Price";
//Definging row
string[] row = new string[] { "1", "Pen", "55.52" };
//adding row to gridview
dataGridView1.Rows.Add(row);
row = new string[] { "2", "Pen", "40" };
dataGridView1.Rows.Add(row);
row = new string[] { "3", "Copy", "20" };
dataGridView1.Rows.Add(row);
row = new string[] { "4", "Copy", "20" };
dataGridView1.Rows.Add(row);
row = new string[] { "5", "Pencil", "120" };
dataGridView1.Rows.Add(row);
}
private void button1_Click(object sender, EventArgs e)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
string cellValue = null;
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
if (!dataGridView1.Rows[i].IsNewRow)
{
cellValue = dataGridView1[1, i].Value.ToString();
if (!dic.ContainsKey(cellValue))
{
dic.Add(cellValue, 1);
}
else
{
dic[cellValue] += 1;
}
}
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("Number of unique products:");
foreach (KeyValuePair<string, int> keyvalue in dic)
{
sb.AppendLine(string.Format("{0}: {1}", keyvalue.Key, keyvalue.Value));
}
lblUniqueProducts.Text = sb.ToString();
lblTotalProducts.Text = "No Of Total Products: "+dic.Count.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
decimal sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
decimal dec = 0;
decimal.TryParse(this.dataGridView1[2, i].FormattedValue.ToString(), out dec);
sum += dec;
}
sum = Math.Round(sum);
lblTotalPrice.Text = "Total Amount: "+sum.ToString("0.00");
}
เอาไปประยุกต์ดูนะ
|
|
|
|
|
Date :
2016-06-10 23:12:15 |
By :
bigsuntat |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|