WebForm Data Grid Button column customization

Discussion in 'ASP .Net Web Controls' started by PatLaf, Jan 27, 2004.

  1. PatLaf

    PatLaf Guest

    I posted this question originally but wasn't clear enough with my request. I was pointed to the winforms help for the data grid. I need to create a web forms datagrid with two button columns that will either decrement or increment the values in the cells in the rows the buttons are in. Is there a sample somewhere I could look at or could someone show me a simple example of how to do this with a web form datagrid?


    Thanks in advance,
    Pat
    PatLaf, Jan 27, 2004
    #1
    1. Advertising

  2. PatLaf

    MSFT Guest

    Hi Pat,

    Thank you for using the community. I am Luke and I am review this issue
    currently. As I understand, you need three column in a web datagrid: one is
    for a number and two button to increase/decrease the number. I compose such
    a sample as following, you may refer to it:

    Here is HTML code:

    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 80px; POSITION:
    absolute; TOP: 136px"
    runat="server" Width="328px" Height="200px"
    AutoGenerateColumns="False">
    <Columns>
    <asp:ButtonColumn CommandName="Increase" ButtonType="PushButton"
    HeaderText="+" Text="Increase" />
    <asp:ButtonColumn CommandName="Decrease" ButtonType="PushButton"
    HeaderText="-" Text="Decrease" />
    <asp:BoundColumn DataField="IntValue"
    HeaderText="value"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>

    Here is the C# code in codebehind:

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here

    if (!IsPostBack)
    {
    DataTable dt = new DataTable("MyTable");

    dt.Columns.Add("IntValue",System.Type.GetType("System.Int16"));

    DataRow dr= dt.NewRow();

    dr[0]=1;

    dt.Rows.Add(dr);

    dr= dt.NewRow();

    dr[0]=2;

    dt.Rows.Add(dr);

    DataGrid1.DataSource =dt;

    DataGrid1.DataBind();

    }




    }


    private void InitializeComponent()
    {
    this.DataGrid1.ItemCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCom
    mand);

    this.Load += new System.EventHandler(this.Page_Load);

    }


    private void DataGrid1_ItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {

    int ovalue;

    ovalue=int.Parse(((TableCell)e.Item.Controls[2]).Text);

    if (e.CommandName=="Increase")
    {
    ((TableCell)e.Item.Controls[2]).Text=(ovalue+1).ToString();
    }
    else
    {
    ((TableCell)e.Item.Controls[2]).Text=(ovalue-1).ToString();
    }




    }


    The Key is we add two Buttoncloumns to datagrid and hand their click event
    in datagrid's ItemCommand.

    Hope this help,

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    MSFT, Jan 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C Newby
    Replies:
    7
    Views:
    560
    C Newby
    Apr 2, 2004
  2. carlos perez
    Replies:
    0
    Views:
    405
    carlos perez
    Jun 8, 2004
  3. Jon

    Datagrid column customization

    Jon, Jan 4, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    388
  4. DBLWizard

    Add Button column to Grid with Data

    DBLWizard, Apr 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    515
    Alejandro Penate-Diaz
    Apr 4, 2005
  5. Jodi
    Replies:
    0
    Views:
    149
Loading...

Share This Page