Problem with DataGrid and values that are empty when AutoGenerateColumns="False"

Discussion in 'ASP .Net Datagrid Control' started by Emil O, Sep 2, 2003.

  1. Emil O

    Emil O Guest

    Hi!

    Im currently writing my Master Thesis in Computer Science and i have
    get caught in a strange DataGrid problem.

    Im using a DataGrid with 4 visible columns and 1 hidden column. The
    first column in both DataGrids are a LinkButton that displays "More
    Info". When im using AutoGenerateColumns="True" everything works
    fine, getting the values from the DataGrid with
    DataGrid1.SelectedItem.Cells[indexValue].Text is no problem at all. So
    everything is fine accept one thing, i cant hide a column(userName
    column).

    So because of that i use a DataGrid with AutoGenerateColumns="False"
    and the use ofTemplateColumns so i can hide the userName column. The
    DataGrid generates correctly with all data in its rows and so on. BUT
    when im trying to get the data from the DataGrid by using
    DataGrid1.SelectedItem.Cells[indexValue].Text all i get is an empty
    value. Very strange, the data is in the DataGrid but i can't get them!

    I have also tried to get the values by using the OnItemCommand but
    with the same result.

    So, i hope you guys know whats wrong. I paste some parts of my code
    below.

    Code in aspx.cs:

    // Page_Load

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    DataView dv = (DataView)buildSearchResult();

    DataGrid1.DataSource = dv;
    DataGrid1.DataBind();
    DataGrid1.Visible = true;
    }

    }

    // Bulding the data in the DataGrid

    private DataView buildSearchResult()
    {
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    DataRow dr = null;

    // Define table column names and datatypes
    DataColumn dc = new
    DataColumn("Vendor",Type.GetType("System.String"));
    dt.Columns.Add(dc);
    dc = new DataColumn("Hits",Type.GetType("System.Int32"));
    dt.Columns.Add(dc);
    dc = new DataColumn("%",Type.GetType("System.Double"));
    dt.Columns.Add(dc);
    dc = new DataColumn("userName",Type.GetType("System.String"));
    dt.Columns.Add(dc);

    // Fill the table with sample data
    for(int i=0;i < 8;i++)
    {
    // Example values, will be taken from database.
    dr = dt.NewRow();
    dr["Vendor"] = "vendorName" +i;
    dr["Hits"] = 100;
    dr["%"] = Math.Round(25.0, 1);
    dr["userName"] = "userName" +i;
    dt.Rows.Add(dr);
    }

    // Return the dataset to the caller
    ds.Tables.Add(dt);

    DataView dv = dt.DefaultView;
    // By default, the first column sorted ascending.
    dv.Sort = "% DESC";

    return dv;
    }

    // Called when OnSelectedIndexChanged in DataGrid1

    public void Selected_Changed(Object sender, EventArgs e)
    {
    // Display the selected userName in the Label named Message
    Message.Text = "User -> " + DataGrid1.SelectedItem.Cells[4].Text;
    Message.Visible = true;
    }

    Code in .aspx:
    // The "working" AutoGenerateColumns="True" DataGrid. Styles and other
    properties removed to reduce code.

    <asp:datagrid id="DataGrid1" runat="server"
    OnSelectedIndexChanged="Selected_Changed" AutoGenerateColumns="True">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:LinkButton id="SelectButton" Text="More info" Width="75"
    CommandName="Select" runat="server" />
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>

    // The DataGrid i can't get the values from. Styles and other
    properties removed to reduce code.

    <asp:datagrid id="DataGrid2" AutoGenerateColumns="False"
    OnSelectedIndexChanged="Selected_Changed">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:LinkButton id="SelectButton" Text="More info" Width="75"
    CommandName="Select" runat="server" />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Vendor">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Vendor") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Hits">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "Hits") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="%">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "%") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn Visible="False">
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "userName") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>

    Hope you can understand the code and see what im doing wrong.

    Best Regards,
    Emil O
    Sweden
     
    Emil O, Sep 2, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    681
  2. =?Utf-8?B?U2Vhblc=?=
    Replies:
    0
    Views:
    692
    =?Utf-8?B?U2Vhblc=?=
    Apr 10, 2007
  3. pietaster
    Replies:
    1
    Views:
    160
    Bernard O'Flynn
    Oct 28, 2003
  4. pietaster

    AllowPaging and AutoGenerateColumns = false

    pietaster, Oct 31, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    171
    Giorgio Parmeggiani
    Oct 31, 2003
  5. Chance Hopkins

    AutoGenerateColumns won't set to false

    Chance Hopkins, Nov 6, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    163
    Alvin Bruney [MVP]
    Nov 6, 2004
Loading...

Share This Page