questions about datagrid

Discussion in 'ASP .Net Datagrid Control' started by Sam, Apr 22, 2004.

  1. Sam

    Sam Guest

    1. how to disable "show navigations button" by program?
    2. how to set position/location (top and left corner) of datagrid by
    program?
     
    Sam, Apr 22, 2004
    #1
    1. Advertisements

  2. you can hide the pager as one approach

    you can use attributes and top, left to position the grid appropriately
     
    Alvin Bruney [MVP], Apr 22, 2004
    #2
    1. Advertisements

  3. Hi Sam,

    I will answer your question one by one:

    1. If you disable the "show navigations button", asp.net designer will add
    attribute to your html view at design-time like this:
    <PagerStyle Visible="False"></PagerStyle>

    So, at run-time, you can diable this function dynamically, through:
    DataGrid1.PagerStyle.Visible=false;

    2. The top and left postion of datagrid control is initialized at
    design-time based on the position you placed it. So you will see that
    designer will add style attribute "left" and "top" like this:
    <asp:DataGrid id="DataGrid2" style="Z-INDEX: 103; LEFT: 248px; POSITION:
    absolute; TOP: 80px"

    So, at run-time, you should change the "left" and "top" attribute of
    datagrid control like this:
    private void Button1_Click(object sender, System.EventArgs e)
    {
    DataGrid1.Style["left"]="0";
    DataGrid1.Style["top"]="0";
    }

    ==================================
    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.
     
    Jeffrey Tan[MSFT], Apr 23, 2004
    #3
  4. Sam

    Sam Guest

    thank's a lot, it's very helpful and i would like to know, where can i find
    information about DataGrid1.Style["left"] in msdn library?
    i mean in visual sudio net, context help only display
    datagrid1.Style() = system.Web.UI.CssStyleCollection

    where can i find information that attribute "left" is part of
    CssStyleCollection? because i still can not set datagrid position using the
    syntax.
     
    Sam, Apr 23, 2004
    #4
  5. Hi Sam,

    Thanks very much for your feedback.

    I am glad I can help you :)

    Are you sure you can not use my code snippet to work? It works well on my
    machine.

    All Asp.net web control will render to client as html element, and all the
    controls' behavior is determined by the result html code. DataGrid
    encapsulates the <table> html element, and some of the properties of
    datagrid also encapsulate the html attributes of <table> element.

    For example:
    DataGrid.BackColor property encapsulate <table>'s
    style="background-color:some_value;" attrubte.

    But, DataGrid did not encapsulate all the style attribute of <table>, other
    attributes(such as top, left) are encapsulated in DataGrid.Style property.
    So you will see in MSDN for Style property:
    "Gets a collection of text attributes that will be rendered as a style
    attribute on the outer tag of the Web server control."

    So any style attributes that can not find corresponding property of that
    web control can be manipulated through Style property. Because DataGrid has
    no property to manipulate "Left" and "Top" style attribute, we can use
    Style property to manipulate them.

    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.
     
    Jeffrey Tan[MSFT], Apr 23, 2004
    #5
  6. Sam

    Sam Guest

    i'm sorry, i forget to tell you that i created datagrid on the fly, see
    example below.
    i have tried the code on different situation (i mean there is an instance of
    datagrid in webform), and it works ;-)
    Public Class WebForm1

    Inherits System.Web.UI.Page

    Dim abc() As Integer = {1, 2, 3, 4, 5}

    Dim dg As DataGrid

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    dg = New DataGrid

    dg.Style("left") = 200 ' this code doesn't work! why ?

    dg.AutoGenerateColumns = True

    dg.Visible = True

    dg.DataSource = abc

    Me.Controls.Add(dg)

    Page.DataBind()

    End Sub
     
    Sam, Apr 24, 2004
    #6
  7. Sam

    Sam Guest

    finally i've got the answer: i need to add style("position"). what do you
    think ?
    can i asked how to add button to datagrid (the datagrid creted on the fly) ?
    i got error says:
    Control '_ctl0__ctl3__ctl0' of type 'Button' must be placed inside a form
    tag with runat=server.

    my source program to add button:
    Dim c6 As New System.Web.UI.WebControls.ButtonColumn

    c6.ButtonType = ButtonColumnType.PushButton

    c6.HeaderText = "View"

    c6.CommandName = "view"

    datagrid1.Columns.Add(c6)

    thanks,

     
    Sam, Apr 24, 2004
    #7
  8. forms1.controls.add(control here) is what you need, roughly

    --
    Regards,
    Alvin Bruney
    [ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
    Got tidbits? Get it here... http://tinyurl.com/27cok
     
    Alvin Bruney [MVP], Apr 25, 2004
    #8
  9. Hi Sam,

    Thanks for your feedback.

    Oh, yes, this is the key-point. This is determined by the client side html
    behavior. If you created the datagrid dynamically, it will default have no
    "position" attribute. And the default "position" attribute will be "static"
    value, which means: Object has no special positioning; it follows the
    layout rules of HTML. For more information, please refer to:
    http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/positio
    n.asp

    So, you have to explicitly add the "position:absolute" attribute for
    datagrid.

    If you want to know more about position in html behavior, please refer to:
    "About Element Positioning"
    http://msdn.microsoft.com/library/default.asp?url=/workshop/author/position/
    positioning.asp

    For your further problem about adding button column into datagrid, I think
    the code you pasted should be correct. But how do you add your datagrid to
    the control tree of the web form? You should add datagrid to the child
    controls of the Form, because web control can only be nested in the <form>
    tag. This works:

    Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
    Private Sub Page_Load(sender As Object, e As System.EventArgs)
    If Not Me.IsPostBack Then
    Dim ds As New DataSet()
    Dim adapter As New SqlDataAdapter("select * from jobs",
    "server=localhost;database=pubs;uid=sa;pwd=")
    adapter.Fill(ds)

    Dim dg As New DataGrid()
    dg.Style("left") = "200px"
    dg.Style.Add("position", "absolute")

    Dim bc As New ButtonColumn()
    bc.ButtonType = ButtonColumnType.PushButton
    bc.Text = "ViewButton"
    bc.HeaderText = "View"
    bc.CommandName = "View"
    dg.Columns.Add(bc)

    Form1.Controls.Add(dg)
    dg.DataSource = ds
    dg.DataBind()
    End If
    End Sub 'Page_Load

    Note: in the code, I get a reference of the <form> tag in code behind
    using: Protected Form1 As System.Web.UI.HtmlControls.HtmlForm, "Form1" is
    the id of the <form> tag. (Because the id is the same, the asp.net will
    automatically associate Form1 reference with this <form> tag)

    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.
     
    Jeffrey Tan[MSFT], Apr 26, 2004
    #9
  10. Sam

    Sam Guest

    thanks for the answer, it's great !
    but i would like to know (maybe you know), why microsoft doesn't make coding
    more easier? i mean why we need to to add "Protected Form1 As
    System.Web.UI.HtmlControls.HtmlForm" ?
    and could you tell me reference about it in msdn ?

    - is there a way to adjust the height (or others parameters) of button in
    datagrid ?
    tks,
     
    Sam, Apr 26, 2004
    #10
  11. Sam

    Sam Guest

    should i used template column to adjust button's height in datagrid ?
    tks,

     
    Sam, Apr 26, 2004
    #11
  12. Sam

    Sam Guest

    i have tried using template column to create button and it works, i mean to
    adjust button's height, but how to make it responds to click event ?
    thks,

     
    Sam, Apr 26, 2004
    #12
  13. Hi Sam,

    Thanks very much for your feedback.

    Yes, for html control, the VS.net default will not add its reference in
    code behind, I think this is by design. Anyway, if you feel un-confortable
    about it, you may provide your suggestion to:
    http://register.microsoft.com/mswish/suggestion.asp

    For its reference, you may just search System.Web.UI.HtmlControls.HtmlForm
    in MSDN, it is here:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfsystemwebuihtmlcontrolshtmlformclasstopic.asp

    For your another question about how to adjust button height in datagrid, I
    will reply to another message of you.

    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.
     
    Jeffrey Tan[MSFT], Apr 28, 2004
    #13
  14. 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.
     
    Jeffrey Tan[MSFT], Apr 28, 2004
    #14
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.