I have a custom control that takes xml data in a certain format and outputs that data using a datagrid. Most of the information is just plain text, but some of it needs to be editable, so sometimes I need to add a control to a given DataColumn at runtime.
Code Csharp:ICollection BuildGrid(string xml_string) { XmlDocument xml = new XmlDocument(); DataTable dt = new DataTable(); DataRow dr; int colCnt; xml.LoadXml(xml_string); GridColumns = GetGridColumns(xml); colCnt = GridColumns.Count; for (int i = 0; i < colCnt; i++) dt.Columns.Add(new DataColumn(GridColumns[i], typeof(string))); for (int i = 0, cnt = xml.FirstChild.ChildNodes.Count; i < cnt; i++) { dr = dt.NewRow(); XmlNodeList lst = xml.FirstChild.ChildNodes[i].ChildNodes; if (lst.Count != colCnt) { throw new XmlException("XML not well-formed!"); } //Here is the important bit! for (int j = 0; j < colCnt; j++) dr[j] = lst[j]; dt.Rows.Add(dr); } DataView dv = new DataView(dt); return dv; }
That's the code that takes the xml, converts it to an XML object, then creates the grid programatically. The loop marked with "here's the important bit" takes the string value lst[j], and assigns it to DataColumn dr[j] -- my question is, how could I write something like this:
Code Csharp:for (int j = 0; j < colCnt; j++) if (lst[j].IsTextboxField()) { Textbox txt = new Textbox(); txt.Text = lst[j]; dr[j].Controls.Add(txt); } else { dr[j] = lst[j]; }




Bookmarks