设置DataGrid的格式问题,dataList.TableStyles[0].GridColumnStyles.Count为什么等于0,请看代码.
DataGridTableStyle ts = new DataGridTableStyle();
ts.AlternatingBackColor = Color.LightYellow;
ts.RowHeadersVisible = false;
dataList.TableStyles.Clear();
dataList.TableStyles.Add(ts);
int count = dataList.TableStyles[0].GridColumnStyles.Count;
int width = dataList.Size.Width;
int n1 = dt.Tables[0].Columns.Count;
int n2 = dataList.TableStyles[0].GridColumnStyles.Count;
dataList.TableStyles[0].GridColumnStyles[0].Width =width/100*10;
dataList.TableStyles[0].GridColumnStyles[1].Width =width/100*10;
dataList.TableStyles[0].GridColumnStyles[2].Width =width/100*90;
dataList.TableStyles[0].AllowSorting = false;
这段代码以前一直在用,可是这次拷过来就用不了了,找了一下原因是因为dataList.TableStyles[0].GridColumnStyles.Count等于0,所以dataList.TableStyles[0].GridColumnStyles[0].Width =width/100*10;就越界了,可是dt.Tables[0].Columns.Count的值等于3,请问高手们大概是怎么回事,应该怎么改.谢谢.
------解决方案--------------------DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "EmployeeInfo ";
DataGridTextBoxColumn TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "EmployeeID ";
TextCol.HeaderText = "Employee ID ";
TextCol.Width = 80;
ts1.GridColumnStyles.Add(TextCol);
DataGrid1.TableStyles.Add(ts1);