0
如何使用LINQ數據源將數據填充到GridView中。 目前我正在使用本手冊,其代碼如下。 Asp.net GridView使用LINQ填充數據DataSource
這裏是我的代碼:
<asp:GridView ID="gridProcesses" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" Width="400px" DataKeyNames="ID">
<Columns>
<asp:BoundField HeaderText="Name" />
<asp:BoundField HeaderText="CPU" />
<asp:BoundField HeaderText="RAM" />
<asp:CommandField ButtonType="Button" SelectText="Kill" ShowSelectButton="True">
<ItemStyle HorizontalAlign="Center" Width="30px" />
</asp:CommandField>
</Columns>
</asp:GridView>
代碼背後
public partial class OsControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
private List<string> getTestData()
{
List<string> tData = new List<string>();
Random rand = new Random();
for (int i = 0; i < 10; i++)
{
tData.Add("proc" + i + "_" + rand.Next(100) + "_" + rand.Next(100));
}
return tData;
}
protected void btnLoad_Click(object sender, EventArgs e)
{
DataTable dtProcesses = new DataTable();
dtProcesses.Columns.Add("Name", System.Type.GetType("System.String"));
dtProcesses.Columns.Add("CPU", System.Type.GetType("System.Int32"));
dtProcesses.Columns.Add("RAM", System.Type.GetType("System.Int32"));
dtProcesses.Columns.Add("ID", System.Type.GetType("System.Int32"));
int id = 0;
foreach (string line in getTestData())
{
string[] items = line.Split('_');
DataRow row = dtProcesses.NewRow();
row["Name"] = items[0];
row["CPU"] = int.Parse(items[1]);
row["RAM"] = int.Parse(items[1]);
row["ID"] = id++;
dtProcesses.Rows.Add(row);
}
gridProcesses.DataSource = dtProcesses;
gridProcesses.DataBind();
}
}-
嘗試此鏈接HTTP: //aspalliance.com/1427_LINQ_to_SQ L_Part_5__Binding_UI_using_the_ASPLinqDataSource_Control.3 http://reblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx – Singleton 2010-11-06 15:25:38