من یه راهی بود خیلی وقا پیش برای رفع این مشکل رفته بودم میزارم اینجا شاید به کارت اومد - اگه سوال داشتی بگو توضیح بدم.
DataTable dt = new DataTable();
dt = search.FindBuildingOrKhak(se);
DataTable tempdt = new DataTable();
tempdt.Columns.Add("کد", typeof(string));
tempdt.Columns.Add("نام مالک", typeof(string));
tempdt.Columns.Add("برای", typeof(string));
tempdt.Columns.Add("قسمت", typeof(string));
tempdt.Columns.Add("منطقه", typeof(string));
tempdt.Columns.Add("متراژ", typeof(string));
DataRow tempRow;
//DataLayer._Owner towner = new DataLayer._Owner();
foreach (DataRow r in dt.Rows)
{
//if (r["status"].ToString() == "ازاد")
// {
tempRow = tempdt.NewRow();
tempRow[0] = r["id"].ToString();
//towner = owner.GetOwner(r["ownerid"].ToString());
tempRow[1] = (towner.name + " " + towner.family);
tempRow[2] = r["_for"].ToString();
tempRow[3] = r["price"].ToString();
tempRow[4] = (region.GetName(r["regionid"].ToString()));
tempRow[5] = (r["area"].ToString());
tempdt.Rows.Add(tempRow);
//}
}
dataGridView1.DataSource = tempdt;
توی تابع search.FindBuildingOrKhak(se);
یه دیتا تیبل بر می گردونه که البته اسم ردیف ها مشخص هست و باید بدونی....
tempdt یه دیتا تیبل واسط هست که سطونهایی رو که لازم دارم توش ایجاد میکنم و در حلقه پایینی اطلاعاتی رو که از تابع بالا خونده بودم بهش اضافه می کنم.
شما سطونی رو که نیاز داری tempdt.Columns.Add(stirngname, typeof(string));
رو اضافه کن و در حلقه عدد مورد نظر رو بهش بده
البته شما if داخل حلقه رو نیاز ندارید.