How to bind DataRow[] as a datasource with the desired display field?
-
Hi, After i execute a select against my DataTable I get a DataRow[] which I bind to a control, for instance a DropDownList. The data schema is lost, because only a DataTable holds columns, due this i have a problem to bind the data as desired: cmb.DataSource = dataRows[]; cmb.DataTextField = "[0]" // should mean row["text"], but there is no text column How do I bind the "Text" Column to a DropDownList after I executed a DataTable.Select()? Because the above code will not work. Any hints appreciated myMsg.BehindDaKeys = "Jerry Maguire";
-
Hi, After i execute a select against my DataTable I get a DataRow[] which I bind to a control, for instance a DropDownList. The data schema is lost, because only a DataTable holds columns, due this i have a problem to bind the data as desired: cmb.DataSource = dataRows[]; cmb.DataTextField = "[0]" // should mean row["text"], but there is no text column How do I bind the "Text" Column to a DropDownList after I executed a DataTable.Select()? Because the above code will not work. Any hints appreciated myMsg.BehindDaKeys = "Jerry Maguire";
That won't work because the index is a numeric and you are passing it in as a string. (The code is looking for a column named [0], not at index Zero. You can try this DataRow[] drs = dataRows[]; cmb.DataSource = drs; cmb.DataTextField = drs[0]; Otherwise you might consider doing it the programmatic way by looping through all of your rows and then DataRow[] drs = dataRows[]; foreach(DataRow dr in drs) { cmb.Items.Add(new System.Web.UI.WebControls.ListItem(MyText, dr[0].ToString())); } Torin Blair
'In the immortal words of Socrates - "I drank what?".'