Re: questions about datagrid

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jeffrey Tan[MSFT] (v-jetan_at_online.microsoft.com)
Date: 04/28/04


Date: Wed, 28 Apr 2004 01:42:17 GMT

Hi Sam,

I will help you another button height and event handle question in this
reply.

To manipulate the child controls in datagrid, normally, you have 2 ways:
declare and dynamic refer it.

To declare refer it, you should leverage TemplateColumn and declare its
height in the <asp:Button> tag. Also, you may add the button's click event
handler through declare. Like this:
<asp:datagrid id="DataGrid1" runat="server">
<Columns>
        <asp:TemplateColumn>
                <ItemTemplate>
                <asp:Button ID="bt" Height="40px" OnClick="ClickEventHandler"
Runat="server" Text="Button"></asp:Button>
                </ItemTemplate>
        </asp:TemplateColumn>
</Columns>
</asp:datagrid>

Then, you should write a method named "ClickEventHandler" to handle the
Click event.(Note: if you writen ClickEventHandler in code behind, you
should mark it as "protected" or "public", you should not specify it as
"private", or a run-time error will generate)

Also, you may not use TemplateColumn, just use a ButtonColumn. Then you can
not use declare way to manipulate it. You have to manipulate it dynamically
at run-time in DataGrid.ItemCreated event. Like this:

private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
        DataGridItem dgi=e.Item;
        if(dgi.ItemType ==ListItemType.Item ||dgi.ItemType
==ListItemType.AlternatingItem)
        {
                                //I suppose your ButtonColumn is the first
column in DataGrid
                foreach(Control c in dgi.Cells[0].Controls)
                {
                        if(c is Button)
                        {
                                Button bt=(Button)c;
                                bt.Height=Unit.Pixel(40);
                                bt.Click +=new EventHandler(bt_Click);
                        }
                }
        }
}

private void bt_Click(object sender, EventArgs e)
{
}

Also, because DataGrid has exposed an event of DataGrid.ItemCommand, which
occurs when any button is clicked in the DataGrid control. So to handle the
Button click in the DataGrid, you can just handler DataGrid.ItemCommand
event. In this event, in order to determine which row's button is clicked,
you can just use DataGridCommandEventArgs.Item.ItemIndex

Like this:

private void DataGrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
        this.Response.Write("ItemCommand +"+ e.Item.ItemIndex.ToString());
}

================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.



Relevant Pages

  • Re: questions about datagrid
    ... > To manipulate the child controls in datagrid, normally, you have 2 ways: ... > declare and dynamic refer it. ... if you writen ClickEventHandler in code behind, ... > private void DataGrid1_ItemCreated(object sender, ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)
  • Re: Stupid Object declaratoin question
    ... Standard datagrid doesn't have buttons, ... declare it when the button is clicked itself... ... > If the user do the searching, I will decarre a form and show it. ... > 1st) Dim frmSearch as New frmCompanySearch ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Big Red X on DataGrid
    ... > How is this a bug in the datagrid? ... > the datagrid and these notifications will occur on the background thread ... > answer is to not manipulate the datatable that the datagrid is bound to ... >> an error in the paint procedure. ...
    (microsoft.public.dotnet.framework.windowsforms.databinding)
  • big ints with gcc under windows ?
    ... I need to manipulate big ints under windows with gcc: ... architectures such as a pentium3. ... - - how to declare such variables? ... Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org ...
    (comp.programming)
  • Re: Data as Global vs passing as argument vs ...
    ... One thing to bear in mind is the fact that Matlab seemingly does not create a copy of an passed argument, as long as you do not manipulate the argument passed, e.g.: ... disp(arg); % arg is still reference ... - If you do share large variables between functions, but you do not manipulate this data within the function, there is no real need to declare them as global, because data will be passed by reference and performance will not decrease. ...
    (comp.soft-sys.matlab)