1
Answer

Problem with GridView (invisible columns)

Tosek Dee

Tosek Dee

18y
7.7k
1
Hi there,

I have a problem with the ASP.NET 2.0 GridView control.

I try to access a cell of the gridview within a postback event and read the cell's text. All this works fine, until I declare the column of the cell as invisible (visible=false). Then I always get an empty string back. If I make the cell visible, I get the correct content from the cell.

This is my website:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BrokerTest.aspx.cs" Inherits="BrokerTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Unbenannte Seite</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"

Height="39px" Style="z-index: 100; left: 0px; position: absolute; top: 0px"

Width="100%" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" AutoGenerateSelectButton="True" Caption="Ergebnis" CaptionAlign="Left" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" ShowFooter="True" DataKeyNames="a,b" OnRowCommand="GridView1_RowCommand">

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<Columns>

<asp:BoundField DataField="a" HeaderText="Dokument">

<HeaderStyle HorizontalAlign="Left" />

</asp:BoundField>

<asp:BoundField DataField="b" HeaderText="Quelle" Visible="False">

<HeaderStyle HorizontalAlign="Left" />

</asp:BoundField>

<asp:ButtonField ButtonType="Button" CommandName="show" HeaderText="Anzeigen" Text="Anzeigen" ShowHeader="True">

<ItemStyle HorizontalAlign="Center" Width="40px" />

</asp:ButtonField>

</Columns>

<RowStyle BackColor="#EFF3FB" />

<EditRowStyle BackColor="#2461BF" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

<asp:Label ID="Label1" runat="server" Style="z-index: 102; left: 18px; position: absolute;

top: 456px" Text="Label" Width="229px"></asp:Label>

</div>

</form>

</body>

</html>



And this is my populate and read content code:

private DataTable CreateTable()

{

try

{

DataTable table = new DataTable();

// Declare DataColumn and DataRow variables.

DataColumn column;

// Create new DataColumn, set DataType, ColumnName

// and add to DataTable.

column = new DataColumn();

column.DataType = System.Type.GetType("System.String");

column.ColumnName = "a";

table.Columns.Add(column);

// Create second column.

column = new DataColumn();

column.DataType = Type.GetType("System.String");

column.ColumnName = "b";

table.Columns.Add(column);

// Create second column.

column = new DataColumn();

column.DataType = Type.GetType("System.String");

column.ColumnName = "c";

table.Columns.Add(column);

return table;

}

catch (Exception ex)

{

throw new Exception(ex.Message);

}

}

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

DataTable dc = CreateTable();

DataRow dr;

dr = dc.NewRow();

dr["a"] = "Hallo1";

dr["b"] = "Hallo2";

dr["c"] = "Path1";

dc.Rows.Add(dr);

dr = dc.NewRow();

dr["a"] = "Hallo3";

dr["b"] = "Hallo4";

dr["c"] = "Path2";

dc.Rows.Add(dr);

 

 

GridView1.DataSource = dc;

GridView1.DataBind();

}

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName == "show")

{

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow selectedRow = GridView1.Rows[index];

TableCell a = selectedRow.Cells[2];

String s = a.Text;

Label1.Text = "<b>" + s + "</b>";

}

}



The method GridView1_RowCommand returns an empty string.

When I change the 2nd column like this:

<asp:BoundField DataField="b" HeaderText="Quelle" Visible="True">

<HeaderStyle HorizontalAlign="Left" />

</asp:BoundField>

the correct content is returned.

But I have to make this column invisible, because I need to store extra data with every row that I don't want to show in the gridview.

Any idea what I am doing wrong?

Answers (1)