Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have gridview with bound field columns. My question is how can I use data for condition that is not a bound field. For example, query is:

Select a1,a2,a3,a4 from table;

But only a1,a2,a3 are bound fields and they are only required to be shown. But value of a4 field is to be used as condition for rowDataBound event. Then how can we retrieve a4 data in a string for each row in rowDataBound event{code}??

I am using dataset to bind data to griview.

Thank You..!!

What I have tried:

tried using viewstate for dataset but it is not working.
Posted
Updated 19-May-16 1:37am

try this,
create a css class to hide the column
HTML
<style>
       .hide {
           display:none
       }
   </style>


Assign the css class to the ItemStyle and HeaderStyle
ASP.NET
<asp:GridView  ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowDataBound="GridView1_RowDataBound">
               <Columns>

                   <asp:BoundField DataField="a1" HeaderText="a1" />
                    <asp:BoundField DataField="a2"  HeaderText="a2" />
                    <asp:BoundField DataField="a3"  HeaderText="a3" />
                   <asp:BoundField DataField="a4" ItemStyle-CssClass="hide" HeaderStyle-CssClass="hide" />

               </Columns>
           </asp:GridView>


Now you can get the value in RowDatabound event

C#
protected void Page_Load(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("a1");
           dt.Columns.Add("a2");
           dt.Columns.Add("a3");
           dt.Columns.Add("a4");
           dt.Rows.Add(1, 2, 3, 4);
           dt.Rows.Add(11, 22, 33, 44);
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }

       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               string a1 = e.Row.Cells[0].Text;
               string a2 = e.Row.Cells[1].Text;
               string a3 = e.Row.Cells[2].Text;
               string a4 = e.Row.Cells[3].Text;

           }
       }
 
Share this answer
 
v2
Comments
planetz 19-May-16 6:34am    
this is rendering an empty column in the middle since there are columns after that column also. Actually, the header is appearing, but the rows are rendering properly
Karthik_Mahalingam 19-May-16 6:52am    
then place this column at the last and change the index accordingly in the code..
planetz 19-May-16 7:20am    
I use the following way: initially visibility of all columns is true.

gv.DataSource = ds.Tables[0];
gv.DataBind();
gv.Columns[7].Visible=false;

Now its working...
Karthik_Mahalingam 19-May-16 7:21am    
ok fine.. cool.
planetz 19-May-16 7:37am    
thanks...!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900