0

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:

  • what is the DataKeyNames ?
  • how would i get this data using LINQ ?

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();
    }
}-
0

2 Answers 2

3

Be sure you specify not just the header text but also the data field:

<asp:BoundField HeaderText="Name" DataField="Name" />
Sign up to request clarification or add additional context in comments.

5 Comments

Great, it works! I added some more questions in my original post if you coul help me with them.
@Primoz - BTW, you might consider changing System.Type.GetType("System.String") to typeof(string). Is cleaner and adds the benefits of compile-time "typo" checking that you don't get with hard coded strings.
You missed DataField="Name" in ` <asp:BoundField HeaderText="Name" />. It should be <asp:BoundField HeaderText="Name" DataField="Name" />` (It is actually in the Phil Hunt's answer).
@Phil Hunt - He forgot to specify DataField.
@a1ex07 - I'm not following your comments on my answer. Primoz's original question was why he was not seeing data (it was due to the absence of DataField="Name"). My answer addressed that, and Primoz confirmed.
1

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:

http://learnentityframework.com/LearnEntityFramework/tutorials/asp-net-databinding-with-linq-to-entities/

1 Comment

I added screenshoot for what i ment with LINQ.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.