Ask a Question related to ASP.NET Data Grid Control, Design and Development.
-
Emil O #1
Problem with DataGrid and values that are empty when AutoGenerateColumns="False"
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 Guest
-
firefox: <param name="menu" value="false" /> !!!
why isn't this working in firefox to prevent menu (i.e. print) ?? Is there something I can do? -
Wierd "go to" problem - false mouseUp?
Hi I have a "go to" command that takes the user to a particular frame in a movie. In that frame waits for a mouseUp (go to frame X) and then... -
Problem with "Custom paging" of DataGrid control
I created a page to show RealEstate Data with images retrived from the MSSQL 2000. I am using a DataGrid control: <asp:datagrid... -
Using Datagrid with EnableViewState="false" gets wrong DataKeys value
I have created a datagrid with search text boxes on the page that allow the user to filter the list. If I use view state, as in the Example 1,... -
"Start" "Program" "Menu" list is empty
For what ever reason my list of installed programs in my "Start" "Programs" menu is empty. Anyone know how to restore the list. Thanks for your...



Reply With Quote

