WebForm Data Grid Button column customization

P

PatLaf

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
 
M

MSFT

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top