josef가 의견에서 언급 한 것처럼 단순히 열을 추가하는 것입니다. GetTableMethod
는 데이터 계층에 연결 한 경우
이 예
더 좋을 것이다, 그러나 그것은 걸쳐 지점 얻어야한다 : 나는 여분의 열을 포함하여 데이터 소스를 제공하는 레이어를 추가 할
private const string sqlConnString = "Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;" +
"User ID=myDomain\myUsername;Password=myPassword;";
DataGrid dataGrid1;
private void BindData() {
var table = GetTableMethod("SELECT * FROM Employees;");
var cInt = table.Columns.Add("My Integer", typeof(int));
var cStr = table.Columns.Add("Hex View", typeof(string));
var cBool = table.Columns.Add("Is Even", typeof(bool));
var dDbl = table.Columns.Add("My Double", typeof(double));
for (int i = 0; i < table.Rows.Count; i++) { // can't use a foreach here
DataRow row = table.Rows[i];
row[cInt] = i;
row[cStr] = i.ToString("X"); // view in hexadecimal, just for fun
row[cBool] = ((i % 2) == 0);
row[dDbl] = (double)i/table.Rows.Count;
}
dataGrid1.DataSource = table;
}
private DataTable GetTableMethod(string sqlSelectText) {
var table = new DataTable();
using (var cmd = new System.Data.SqlClient.SqlCommand(sqlSelectText, new System.Data.SqlClient.SqlConnection(sqlConnString))) {
try {
cmd.Connection.Open();
table.Load(cmd.ExecuteReader());
} finally {
cmd.Connection.Close();
}
}
return table;
}
을. 데이터 소스는 컬렉션으로 볼 수 있습니다. 컬렉션은 다른 출처의 데이터로 채울 수 있습니다. CF의 DataGrid는 내부 편집을 지원하지 않으므로이 단방향 (읽기 전용) 컬렉션의 레이어를 쉽게 코딩 할 수 있어야합니다. – josef
예를 들어 데이터 테이블을 사용하고 DB의 데이터로 채우고 계산 된 데이터에 대한 열을 추가하십시오. 그런 다음 해당 데이터 테이블을 DataGrid의 DataSource로 사용하십시오. – josef