Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hi to all,

Good day to everyone and thank you. I need help on my web app. I'm having a problem getting a value of a cell in a dynamic gridview. can anyone help me with this? thank you very much... the code is right below.
XML
<asp:GridView ID="gvAttendance" runat="Server"
                      AllowPaging="True"
                      AutoGenerateColumns="true" PageSize="10"
                      SelectedIndex="0" Width="465px"
                      CssClass="mGrid" PagerStyle-CssClass="pgr"
                      AlternatingRowStyle-CssClass="alt"

                      DataSourceID="sqlDSAttendance" DataMember="DefaultView"
            onrowdatabound="gvAttendance_RowDataBound"
            onrowcommand="gvAttendance_RowCommand" >
            <EmptyDataTemplate>
                <asp:Label ID="lblNull" runat="Server"
                           ForeColor="Red"> There&#39;s no data to show in this view...
                </asp:Label>
            </EmptyDataTemplate>
        </asp:GridView>
    </div>
</div>
<asp:SqlDataSource ID="sqlDSAttendance" runat="server"
                   ConnectionString="<%$ ConnectionStrings:OTEConnString %>"
                   ProviderName="MySql.Data.MySqlClient"
                   SelectCommand = "CALL ote.pivotwizard('AgentID,AgentNo,Agent_Name','att_Date','att_Time','ote.view_attendance','((agent_name IS NOT NULL) OR (year(att_date)=2010))')">
</asp:SqlDataSource>
Posted
Updated 9-Jul-17 21:02pm
v3
Comments
Sandeep Mewara 27-Oct-10 0:44am    
What problem? Any error? Show us what were you trying?
Abhijit Jana 27-Oct-10 0:54am    
Please show us your code where you are creating the gridview
Dalek Dave 27-Oct-10 3:54am    
Edited for Proper Code Block Language.

Hi
You can get the value of dynamically created gridview like

this will return value of Gridview1 of Row 1 of Cell 1
gridview1.rows[0][0].text


Thanks,
Imdadhusen
 
Share this answer
 
Comments
Ice_Freez05 27-Oct-10 1:41am    
Thanks for that. but that is accessing the gridview row statically.
Sunasara Imdadhusen 27-Oct-10 1:55am    
You can get gridview using loop and finally get value from it
Ice_Freez05 27-Oct-10 1:58am    
How am i supposed to do that? can you give me some example? thank you very much...
Sunasara Imdadhusen 27-Oct-10 2:04am    
Yes sure, please send your code snippet. so i can modify accordingly.
Ice_Freez05 27-Oct-10 3:12am    
I don't know how to start it. I can't even get the onclick event of the cell.
I am not sure what do you mean by dynamucally created grid on that context.

If you are creating control like this way,
GridView grdView= new GridView();
grdView.DataSource=myDataSource;


You can access the GridRows by using GridViewRows Class

GridViewRows gridviewRow= grdView.SelectedRow;


Now from gridviewRow you get value of any cell for that row, You can also go with as Sunasara suggested to get value directly from a Cell.

Hope this will help you !
 
Share this answer
 
Comments
Ice_Freez05 27-Oct-10 1:41am    
I'll be able to do that.but i need to get the specific value of the clicked cell because I'm populating the gridview dynamically and without specific column for the dates column.
Dalek Dave 27-Oct-10 3:55am    
Good Call.
C#
TextBox abc=(TextBox)Gridview1.Rows[e.Rowindex].Cells[0].Controls[0];


this is for first position of gridview binding item.and here you can convert as per your control of choice like textbox, label etc.

another ways is like as
C#
TextBox abc=(TextBox)Gridview1.Rows[e.Rowindex].Cells[0].FindControl("controlname");


as you have used in item template of gridview.
 
Share this answer
 
v2
Comments
Ice_Freez05 27-Oct-10 1:43am    
I can''t use an item template because the 4th column of the gridview is dynamic. I haven't got an idea what would be the column name because I got the data from a stored procedure which transpose the date row to a column.
<pre lang="xml"><asp:GridView ID="gvAttendance" runat="Server"
                      AllowPaging="True"
                      AutoGenerateColumns="true" PageSize="10"
                      SelectedIndex="0" Width="465px"
                      CssClass="mGrid" PagerStyle-CssClass="pgr"
                      AlternatingRowStyle-CssClass="alt"

                      DataSourceID="sqlDSAttendance" DataMember="DefaultView"
            onrowdatabound="gvAttendance_RowDataBound"
            onrowcommand="gvAttendance_RowCommand" >
            <EmptyDataTemplate>
                <asp:Label ID="lblNull" runat="Server"
                           ForeColor="Red"> There&#39;s no data to show in this view...
                </asp:Label>
            </EmptyDataTemplate>
        </asp:GridView>
    </div>
</div>
<asp:SqlDataSource ID="sqlDSAttendance" runat="server"
                   ConnectionString="<%$ ConnectionStrings:OTEConnString %>"
                   ProviderName="MySql.Data.MySqlClient"
                   SelectCommand = "CALL ote.pivotwizard('AgentID,AgentNo,Agent_Name','att_Date','att_Time','ote.view_attendance','((agent_name IS NOT NULL) OR (year(att_date)=2010))')">
</asp:SqlDataSource>



This is my code for binding the gridview. the auto populate is set to true because the data is dynamic when it comes to adding some column. so I need to get the value of the cell that was clicked by the client for update.
 
Share this answer
 
Comments
Abhijit Jana 27-Oct-10 1:54am    
Please Update your origninal post with this source code.
Sunasara Imdadhusen 27-Oct-10 2:10am    
Don't add multiple question every time.
for(int i<gvAttendance.rows.count; i++)
{
Label lblAttendance=(Label)gvAttendance.Rows[i].findcontrol("lblNull");

}
 
Share this answer
 
Comments
Ice_Freez05 27-Oct-10 3:27am    
in what event of gridview will i put that snippet?Thanks!

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