PDA

View Full Version : جستجو در سطرهای DataTable



b581102
چهارشنبه 15 اسفند 1386, 12:36 عصر
من از کد زیر استفاده می کنم :

DataTable dt = BussinessLayer.Instances.Categories.GetCategoriesD ataTable();
dt.PrimaryKey[0] = dt.Columns["catid"];
int parent = Convert.ToInt32(dt.Rows.Find(parentId)["catId"]);


ولی هنگام اجرا خطای زیر را می دهد :

index was outside the bound of arrays

hassan razavi
چهارشنبه 15 اسفند 1386, 12:50 عصر
دوست عزیز برای Set کردن کلیدهای جدول از روش زیر (منبع MSDN( استفاده کنید




private void SetPrimaryKeys()
{
// Create a new DataTable and set two DataColumn objects as primary keys.
DataTable table = new DataTable();
DataColumn[] keys = new DataColumn[1];
DataColumn column;

// Create column 1.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName= "FirstName";

// Add the column to the DataTable.Columns collection.
table.Columns.Add(column);

// Add the column to the array.
keys[0] = column;

// Create column 2 and add it to the array.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "LastName";
table.Columns.Add(column);

// Add the column to the array.
keys[1] = column;

// Set the PrimaryKeys property to the array.
table.PrimaryKey = keys;
}

hassan razavi
چهارشنبه 15 اسفند 1386, 13:05 عصر
یعنی باید کدتون را بدینصورت تغییر بدید


DataColumn[] dcs = new DataColumn[0];
dcs[0]=dt.Columns["catid"];
dt.PrimaryKey = dcs;