Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 668927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:05:42+00:00 2026-05-14T00:05:42+00:00

In runtime I’m creating a DataTable and using nested for-loops to populate the table.

  • 0

In runtime I’m creating a DataTable and using nested for-loops to populate the table. This table I later assign as DataSource to a gridview and on RowDataBound I assign the value of each cell. I want to know how I can give each cell a button and assign that button to a codebehind function. I’ll have 12 buttons and each one will contain a different value. I would prefer if they all call the same function with some kind of event that stores the cell-specific value.

This is the code where the Table gets created:

protected void GridViewDice_RowDataBound(object sender, GridViewRowEventArgs e)
{


    DataTable diceTable = _gm.GetDice(_gameId);
    for (int i = 0; i < GameRules.ColumnsOfDice; i++)
    {
        if(e.Row.RowIndex > -1)
        {
            /*This is where I'd like to add the button*/
            //e.Row.Cells[i].Controls.Add(new Button);
            //e.Row.Cells[i].Controls[0].Text = specific value from below

            //This is where the specific value gets input
            e.Row.Cells[i].Text = diceTable.Rows[e.Row.RowIndex][i].ToString();
        }

    }
}

I would like to handle the buttonclick with something like this:

protected void DiceButton_Click(int column, int row, int value)
{
    //Do whatever
}

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:05:42+00:00Added an answer on May 14, 2026 at 12:05 am

    On your gridview in markup, assign CommandArgument attribute to whichever you want (here I choose the index of the current gridviewrow) inside the your buttons.

     <asp:Button ID="lbnView" runat="server" Text="Button" OnClick="btn_Clicked" 
    CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"></asp:Button>
    

    Or in your code behind, you can create a button like below

    protected void GridViewDice_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
    
    
        DataTable diceTable = _gm.GetDice(_gameId); 
        for (int i = 0; i < GameRules.ColumnsOfDice; i++) 
        { 
            if(e.Row.RowIndex > -1) 
            { 
                Button btn = new Button();
                btn.CommandArgument = diceTable.Rows[e.Row.RowIndex][i].ToString(); 
                btn.Attributes.Add("OnClick", "btn_Clicked");
    
                e.Row.Cells[i].Controls.Add(btn);
            }
        }
    }
    

    then make an event handler like below

    protected void btn_Clicked(object sender, EventAgrs e)
    {
       //get your command argument from the button here
       if (sender is Button)
       {
         try
         {
            String yourAssignedValue = ((Button)sender).CommandArgument;
         }
         catch
         {
           //Check for exception
         }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think this is well supported (see sharing build… May 14, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer Yes, there is. But not in that particular example. If… May 14, 2026 at 6:55 pm
  • Editorial Team
    Editorial Team added an answer To start, you create the label as a property of… May 14, 2026 at 6:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.