Can I set these properties of Gridview at run time.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saifulhaque
    New Member
    • Aug 2010
    • 16

    #1

    Can I set these properties of Gridview at run time.

    I want to set colums, TemplateFied,He aderTemplate,It emTemplate with (LinkButton-Text,CommandNam e,Onclick etc)
    Here I am showing my code t_name table name it is varable,I want to set
    various table contains various field,Then I want generate Dynamically Gridview with the above properties.
    Code:
    string str = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd;
            SqlDataAdapter ad = new SqlDataAdapter();
            con.Open();
            cmd = new SqlCommand(“select * from t_name”, con)
            ad.SelectCommand = cmd;
            DataSet ds = new DataSet();
            ad.Fill(ds, t_name);
                GridView1.DataSource = ds;
                GridView1.DataBind();
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I'm sorry but I don't understand your question.

    Could you please rephrase it and include details on what you are having problems with?

    Also, could you post the ASP markup for the GridView?

    -Frinny

    Comment

    • mzmishra
      Recognized Expert Contributor
      • Aug 2007
      • 390

      #3
      Yes you can create gridview at runtime.
      Here is just sample code how to add bound couln and template column
      Code:
      BoundField Column1 = new BoundField();
      
      Column1.DataField = "Name";
      
      Column1.HeaderText = "Name";
      
      GridView1.Columns.Add(Column1);
      
      TemplateColumn
      
      TemplateField ckhColumn = new TemplateField();
      
      
      ckhColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "CheckBox Column");
      
      ckhColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");
      
      GridView1.Columns.Add(ckhColumn);

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        If you choose to do this (which I do not recommend if you are new to ASP.NET)...and you want to have your LinkButtons work properly, you will need to place this code in the Page Init event, and you will need to make sure that your TemplateFields to gobble up the LinkButton click events...

        You should look over this quick over view of how to use dynamic controls in asp.net to get an idea of what is involved with dynamic controls.

        If you are new to ASP.NET I strongly recommend against dynamically creating your TemplateFields because if the number of TemplateFields changes between postbacks you could run into issues....also you need to have a firm grasp on how events and the ASP.NET life cycle or else you will run into problems.

        I recommend adding your TemplateFields into your GridView ASP markup. This will save you a lot of headaches.

        -Frinny

        Comment

        • saifulhaque
          New Member
          • Aug 2010
          • 16

          #5
          //I want to set these properties of gridview at run time.

          I want to use it various tables.
          Code:
          <form id="form1" runat="server">
              <div>
             
                     &nbsp;
                    <asp:GridView ID="GridView" AutoGenerateColumns="false"  runat="server" BackColor="Azure" BorderColor="#3366CC"
                  BorderStyle="None" BorderWidth="1px" CellPadding="4" Height="1px"
                  AllowPaging="True" PageSize ="5" OnPageIndexChanging="GridView_Paging"     style="left: 3px; top: 185px" CssClass="position" Width="466px"   OnRowCommand ="GridView_RowCommand" OnSelectedIndexChanged="GridView_SelectedIndexChanged">
                  <FooterStyle BackColor="#99CCCC" ForeColor="#003399" Font-Size="10px" />
                  <RowStyle BackColor="White" ForeColor="#003399" Font-Size="10px" Height="8px" />
                  <PagerStyle BackColor="#99CCCC" ForeColor="#8080FF" HorizontalAlign="Left" Font-Size="10px" Height="8px" />
                  <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" Font-Size="10px" />
                  <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" Font-Size="10px" />
                 <Columns>
                  <asp:TemplateField  >
                  
                  <HeaderTemplate >ORDER NO</HeaderTemplate>
                  <ItemTemplate>
                       <asp:LinkButton ID="link" runat ="server"  Text= '<%#DataBinder.Eval(Container.DataItem, "SL_NO")%>' CommandName ='<%#DataBinder.Eval(Container.DataItem, "RID")%>'  OnClick ="Link_Click"></asp:LinkButton>
                  </ItemTemplate>
               
                  </asp:TemplateField>
                  
                   <asp:TemplateField >
                  
                  <HeaderTemplate >ID</HeaderTemplate>
                  <ItemTemplate>
                  <asp:Label ID="lb2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "LOC_ID")%>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                  
                  
                  
                   <asp:TemplateField >
                  
                  <HeaderTemplate >NAME IN ENGLISH</HeaderTemplate>
                  <ItemTemplate>
                  <asp:Label ID="lb4" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "LOC_DE_EN")%>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField >
                  
                  <HeaderTemplate >NAME IN ARABIC</HeaderTemplate>
                  <ItemTemplate>
                  <asp:Label ID="lb5" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "LOC_DE_AR")%>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField >
                  
                  <HeaderTemplate >STATUS</HeaderTemplate>
                  <ItemTemplate>
                  <asp:Label ID="lb6" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "STATUS")%>'></asp:Label>
                  </ItemTemplate>
                  </asp:TemplateField>
                
          
          
          </Columns>
              </asp:GridView>
              </div>
              </form>
          Last edited by Frinavale; Sep 29 '10, 06:36 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            So, you want to use the same GridView to display different tables.....

            I'm just curious, before we begin down this path...
            Why don't you just make a GridView (as you have here) for each table...and set the Visible property of the GridViews to true or false...accordi ng to the table the user is working with?

            -Frinny

            Comment

            • saifulhaque
              New Member
              • Aug 2010
              • 16

              #7
              How can set visible property true and false for diffrent tables,How i can set Header text for each field.If i didn't change Header text field name will be header text Can you give an example.

              If i get the answer of previous one that is also better to me.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                You can change the properties during the RowDataBound event.

                At this point the data has been bound to the row....you can access the type of row being bound to determine the course of action to take.

                -Frinny

                Comment

                • saifulhaque
                  New Member
                  • Aug 2010
                  • 16

                  #9
                  Can you give an example for changing Header Text of columns at run time?

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Hmm, normally I would recommend that you try something yourself first and post what you have so that we can work from there...but here is an example of what you're looking to do.

                    (vb.net)
                    Code:
                    Protected Sub MyGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowDataBound
                      If e.Row.RowType = DataControlRowType.Header Then
                        Dim link As LinkButton = Ctype(e.Row.FindControl("link"), LinkButton)
                        If link IsNot Nothing
                          ' do stuff with the "link" in the header
                        End If
                      End If
                    End Sub
                    -Frinny

                    Comment

                    Working...