如果字符串靜態的(將不添加/由用戶刪除),則最簡單的方式是創建一個列表<>或用於字符串的數組和這樣
string[] m = new string[] { "Guest", "Admin", "Operator", "Unit Manager", "User" }
/// <summary>
///
/// </summary>
/// <param name="m">the string array which searches for the integer criteria.</param>
/// <param name="s"> the int32 number which will pass to the index of the array </param>
/// <returns></returns>
public static string IntToString(this string S, string[] m, int s)
{
string z = m.ElementAt(s);
//Array.Clear(m, 0, m.Length);
/// if you will need to clear the string array elements each using of this method then delete the comment slashes in front of the Array.Clear() method
/// in Array.Clear method also -to depends of your need- you can disable to show the
/// Array elements.. May be you will need only 1 admin and if an admin chooosen you can delete this option by changing the parameters of Array.Clear() Method
return z;
}
的小延伸方法並在數據訪問層類簡單的使用例子:
string g;
if (dataReader["yourIntValueFromTable"] != DBNull.Value)
{
yourClassObject.yourIntValueFromTable = (int)dataReader["yourIntValueFromTable"];
yourClassObject.yourStringValue = g.IntToString(m, ((int)dataReader["yourIntValueFromTable"]));
}
填充後,這個類可以作爲數據源設置爲任何你想要的。
但是,如果你的字符串是動態的,那麼你需要創建一個存儲過程,並從那裏
你問如何進行具體操作,或者只是如何在普遍執行的操作,數據綁定之前打電話給你的價值觀? – Tyrsius
@Tyrsius一般來說。這個具體的操作是我想要它做的一個例子。 – Abion47
這其實很簡單。在將DataGridView的'DataSource'屬性設置爲任何持有數據的對象之前,請執行您需要的任何操作。 – Tyrsius