Hello, I have a function that returns a DataTable in my asp code-behind page but don't know how to render it. So far I've tried calling the function in the Load_Page event but it doesn't work.
I assume I have to return this as part of a ASP control but don't know how.
Could you please help?
Attached is the source.
Loading
VulpesPosted Sep 25, 2011, 6:44 PM
AndyPosted Sep 25, 2011, 3:25 PM
I want to display a button as a column value in my table. I have a function that returns a Button type and set the column type to "System.Web.UI.WebControls.Button" but I get an exception when loading the page:
Exception Details: System.ArgumentException: Column requires a valid DataType.
protected DataTable Table()
{
...
col5.DataType = System.Type.GetType("System.Web.UI.WebControls.Button");
row[col5] = CustomButton("Click me");
}
protected Button CustomButton(string caption)
{
Button btn = new Button();
btn.Text = caption;
return btn;
}
What type do I need to set to display the button in my table? Thanks again!
LE: According to http://msdn.microsoft.com/en-us/library/system.data.datacolumn.datatype.aspx there's no such datatype as Web.UI.WebControls.Button :(
Still not sure how to approach this, a cast of some sort maybe?
AndyPosted Sep 25, 2011, 2:55 PM
Satyapriya NayakPosted Sep 25, 2011, 2:50 PM
Try this,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Table();
}
void Table()
{
DataTable table = new DataTable("emp");
DataColumn col1 = new DataColumn("Column1");
DataColumn col2 = new DataColumn("Column2");
DataColumn col3 = new DataColumn("Column3");
DataColumn col4 = new DataColumn("Column4");
col1.DataType = System.Type.GetType("System.String");
col2.DataType = System.Type.GetType("System.String");
col3.DataType = System.Type.GetType("System.String");
col4.DataType = System.Type.GetType("System.String");
table.Columns.Add(col1);
table.Columns.Add(col2);
table.Columns.Add(col3);
table.Columns.Add(col4);
DataRow row = table.NewRow();
row[col1] = "Content1";
row[col2] = "Content2";
row[col3] = "Content3";
row[col4] = "Content4";
table.Rows.Add(row);
DataSet ds;
ds = new DataSet();
ds.Tables.Add(table);
DataGrid1.DataMember = "emp";
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
}
Thanks
If this post helps you mark it as answer
VulpesPosted Sep 25, 2011, 2:37 PM