DataTableフィルタ処理
datatableを構築するには、次の手順に従います.
方法1:
方法2:
IEqualityComparerインタフェースのメンバーメソッドの実装
DataTable dt = new DataTable();
dt.Columns.Add("customid");
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["customid"] = i.ToString();
dt.Rows.Add(dr);
}
新しい列Addメソッドを追加すればいいです.ある列の後ろに挿入するには、SetOrdinalメソッドを使用します.///
///
///
///
///
private List GetObjItemsFromList(string findstr)
{
string[] arry = { "01 ", "02 ", "03 " };
return arry.ToList().FindAll(delegate(string str) { return str.Contains(findstr); });
}
DataTable dt_excel = ds.Tables[0];
dt_excel.Columns.Add("new_sex").SetOrdinal(3);// new_sex ( )
for (int i = 0; i < dt_excel.Rows.Count; i++)
dt_excel.Rows[i]["new_sex"] = dt_excel.Rows[i]["e_sex"].ToString() == "0" ? " " : " ";
dt_excel.Columns.Remove("e_sex");//
方法1:
using (ds = ....)
{
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
System.Text.StringBuilder temp_dept_id = new System.Text.StringBuilder();
ds.Tables[0].DefaultView.ToTable(true, "HR_DEPT_ID").AsEnumerable().ToList().ForEach(x => { temp_dept_id.Append(FormateAddString(x["HR_DEPT_ID"].ToString())); });
if (string.IsNullOrEmpty(temp_dept_id.ToString())) return;
strWhere += " AND HR_DEPT_ID in(" + temp_dept_id.ToString().TrimEnd(',') + ")";
}
}
///
///
///
private string FormateAddString(string paramstr)
{
return !string.IsNullOrEmpty(paramstr) ? (paramstr + ",") : "";
}
方法2:
IEqualityComparerインタフェースのメンバーメソッドの実装
///
/// DataTable
///
/// IEqualityComparer
public class DataTableRowCompare : IEqualityComparer
{
public DataTableRowCompare()
{
}
public DataTableRowCompare(string filedname)
{
_fieldname = filedname;
}
private string _fieldname = "id";
public string fieldName
{
get { return _fieldname; }
set { _fieldname = value; }
}
public bool Equals(DataRow x, DataRow y)
{
return (x.Field(fieldName) == y.Field(fieldName));
}
public int GetHashCode(DataRow obj)
{
return obj.ToString().GetHashCode();
}
}
using (DataSet postDS = .....)
{
DataTable _resultDt = postDS.Tables[0].AsEnumerable().Distinct(new DataTableRowCompare("POST_NAME")).CopyToDataTable();
CCBindDropDownList(this.ddlPostionName, _resultDt, "POST_NAME", "HR_DEPT_PATH", new ListItem());
}