Reputation: 4321
Why I get just empty rows without any data ?
EDIT:
Now I see my data thanks to Phil. I just want to know two more little things:
DataKeyNames
?Here is my code:
<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>
code behind
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();
}
}-
Upvotes: 0
Views: 1735
Reputation: 8521
Responses to updated questions:
What is DataKeyNames
?
This property gets or sets the primary key fields for items displayed in the GridView:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx
How would i get this data using LINQ?
If using LINQ, you likely are thinking of LINQ to Entities (Entity Framework). Describing EF in detail is a bit large for the scope of an answer, but this should get you started with databinding to EF:
Upvotes: 1
Reputation: 8521
Be sure you specify not just the header text but also the data field:
<asp:BoundField HeaderText="Name" DataField="Name" />
Upvotes: 3