PDA

View Full Version : مرتب كردن عددي DataGridVew



hrbaban
پنج شنبه 24 تیر 1389, 13:18 عصر
با سلام . . .

من يه سري ستون رو در ديتا گريدم دارم . كه تمام اين ستونها در بانك اطلاعاتي از نوع رشته اي هستند اما مقادير عددي در اونها وجود داره .

وقتي اطلاعات در گريد نمايش داده ميشه به صورت رشته اي مرتب ميشه يعني به صورت زير :
1و11و12و2و22و3و33

درصورتي كه من ميخوام به صورت عددي نمايش داده بشوند .
چيكار كنم ؟

hrbaban
شنبه 26 تیر 1389, 11:22 صبح
واقعا تا حالا كسي اين مشكل رو نداشته ؟؟؟؟؟؟؟؟؟

hrbaban
جمعه 01 مرداد 1389, 16:48 عصر
کمک ؟؟؟
حرفه ای ها چطوری دیتا گرید مرتب میکنند ؟

ACorvinus
شنبه 02 مرداد 1389, 08:18 صبح
سلام.

من مشکل شمارو حل کردم. سر درآوردن در کدها رو میذارم به عهده خودتون. بخونین اگه متوجه نشدین سوال بپرسین.

من فرض رو بر این گرفتم که شما دو تا ستون دارین که ستون Num همون ستونی هستش که اعداد رشته ای شما رو نگه میداره.

این کد به دو صورت نوشته شده یعنی هم زمانی که Gridview شما DataSource داره و هم زمانی که DataSource نداره:

struct Info
{
public string Num;
public string Name;
}
private void sortbutton_Click(object sender, EventArgs e)
{
if (this.dataGridView1.DataSource != null && this.dataGridView1.Rows.Count >= 1)
{
SortedList<Int32, Info> slst = new SortedList<Int32, Info>();
int KeyCounter = -1;
foreach (DataRow dr in ((DataTable)this.dataGridView1.DataSource).Rows)
{
KeyCounter++;
Info k = new Info();
k.Num = dr["Num"].ToString();
k.Name = dr["Name"].ToString();

slst.Add(KeyCounter, k);
}

//Sort by Num Column

for (int i = 0; i <= slst.Count - 1; i++)
for (int j = i + 1; j < slst.Count; j++)
{
Info Inf1 = slst[i];
Info Inf2 = slst[j];

if (((Inf1.Num != null) && (Inf1.Num != "")) && ((Inf2.Num != null) && (Inf2.Num != "")))
{
if (Convert.ToInt32(Inf1.Num) > Convert.ToInt32(Inf2.Num))
{
slst.Remove(i);
slst.Remove(j);
Info Temp = Inf1;
Inf1 = Inf2;
Inf2 = Temp;
slst.Add(i, Inf1);
slst.Add(j, Inf2);
}
}
else if (Inf2.Num == null || Inf2.Num == "")
{
slst.Remove(i);
slst.Remove(j);
Info Temp = Inf1;
Inf1 = Inf2;
Inf2 = Temp;
slst.Add(i, Inf1);
slst.Add(j, Inf2);
}
}
//Save Infos To DataTable
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Num"));
dt.Columns.Add(new DataColumn("name"));

for (int i = 0; i <= slst.Count - 1; i++)
{
DataRow dr = dt.NewRow();

dr["Num"] = slst[i].Num.ToString();
dr["name"] = slst[i].Name.ToString();
dt.Rows.Add(dr);
}

this.dataGridView1.DataSource = dt;
}
else if (this.dataGridView1.DataSource == null && this.dataGridView1.Rows.Count >= 1)
{
List<Info> lst = new List<Info>();
if (this.dataGridView1.Rows.Count >= 1)
{
foreach (DataGridViewRow dr in this.dataGridView1.Rows)
{
Info inst = new Info();
if (dr.Cells["Num"].Value == null || dr.Cells["Num"].Value == "")
{
inst.Num = "";
}
else { inst.Num = dr.Cells["Num"].Value.ToString(); }
if (dr.Cells["name"].Value == null || dr.Cells["name"].Value == "")
{
inst.Name = "";
}
else { inst.Name = dr.Cells["name"].Value.ToString(); }


lst.Add(inst);
}
}
//Sort by Num Column
for (int i = 0; i <= lst.Count - 1; i++)
for (int j = i + 1; j < lst.Count; j++)
{
if (((this.dataGridView1.Rows[i].Cells["Num"].Value != null) && (this.dataGridView1.Rows[i].Cells["Num"].Value != "")) &&
((this.dataGridView1.Rows[j].Cells["Num"].Value != null) && (this.dataGridView1.Rows[j].Cells["Num"].Value != "")))
{
if (Convert.ToInt32(this.dataGridView1.Rows[i].Cells["Num"].Value) > Convert.ToInt32(this.dataGridView1.Rows[j].Cells["Num"].Value))
{
string Num = this.dataGridView1.Rows[j].Cells["Num"].Value.ToString();
string Name = string.Empty;
if (this.dataGridView1.Rows[j].Cells["name"].Value != null)
{
Name = this.dataGridView1.Rows[j].Cells["name"].Value.ToString();
}
this.dataGridView1.Rows[j].Cells["Num"].Value = this.dataGridView1.Rows[i].Cells["Num"].Value;
this.dataGridView1.Rows[j].Cells["name"].Value = this.dataGridView1.Rows[i].Cells["name"].Value;
this.dataGridView1.Rows[i].Cells["Num"].Value = Num;
this.dataGridView1.Rows[i].Cells["name"].Value = Name;
}
}
else if ((this.dataGridView1.Rows[j].Cells["Num"].Value == null) || (this.dataGridView1.Rows[j].Cells["Num"].Value == ""))
{

string Num = string.Empty;
string Name = string.Empty;
if (this.dataGridView1.Rows[j].Cells["name"].Value != null)
{
Name = this.dataGridView1.Rows[j].Cells["name"].Value.ToString();
}
this.dataGridView1.Rows[j].Cells["Num"].Value = this.dataGridView1.Rows[i].Cells["Num"].Value;
this.dataGridView1.Rows[j].Cells["name"].Value = this.dataGridView1.Rows[i].Cells["name"].Value;
this.dataGridView1.Rows[i].Cells["Num"].Value = Num;
this.dataGridView1.Rows[i].Cells["name"].Value = Name;
}
}
}
}

ACorvinus
شنبه 02 مرداد 1389, 08:19 صبح
اکثر کدها تکراری هستش میتونی اونارو یه روال کنی که آرگومان میگیره!!!