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

E

Emil O

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top