Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Problem with DataGrid and values that are empty when AutoGenerateColumns="False"

Reply
Thread Tools

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

 
 
Emil O
Guest
Posts: n/a
 
      09-02-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
behavior varied between empty string '' and empty list [] Tzury Bar Yochay Python 1 03-24-2008 06:56 PM
DetailsView ASP Control and an empty field not an empty record. ButlerDJIAM ASP .Net 0 11-09-2006 06:40 PM
DataGrid edited values problem and autogenerated columns in DataGrid problem John Walsch ASP .Net Web Controls 0 07-09-2004 03:03 PM
Check if a directory is empty and empty it Marcia Hon C Programming 8 02-14-2004 03:53 AM



Advertisments