473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can I set these properties of Gridview at run time.

I want to set colums, TemplateFied,HeaderTemplate,ItemTemplate with (LinkButton-Text,CommandName,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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. string str = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
  3.         SqlConnection con = new SqlConnection(str);
  4.         SqlCommand cmd;
  5.         SqlDataAdapter ad = new SqlDataAdapter();
  6.         con.Open();
  7.         cmd = new SqlCommand(“select * from t_name”, con)
  8.         ad.SelectCommand = cmd;
  9.         DataSet ds = new DataSet();
  10.         ad.Fill(ds, t_name);
  11.             GridView1.DataSource = ds;
  12.             GridView1.DataBind();
  13.  
  14.  
  15.  
Sep 29 '10 #1
9 3364
Frinavale
9,735 Expert Mod 8TB
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
Sep 29 '10 #2
mzmishra
390 Expert 256MB
Yes you can create gridview at runtime.
Here is just sample code how to add bound couln and template column
Expand|Select|Wrap|Line Numbers
  1. BoundField Column1 = new BoundField();
  2.  
  3. Column1.DataField = "Name";
  4.  
  5. Column1.HeaderText = "Name";
  6.  
  7. GridView1.Columns.Add(Column1);
  8.  
  9. TemplateColumn
  10.  
  11. TemplateField ckhColumn = new TemplateField();
  12.  
  13.  
  14. ckhColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "CheckBox Column");
  15.  
  16. ckhColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");
  17.  
  18. GridView1.Columns.Add(ckhColumn);
Sep 29 '10 #3
Frinavale
9,735 Expert Mod 8TB
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
Sep 29 '10 #4
//I want to set these properties of gridview at run time.

I want to use it various tables.
Expand|Select|Wrap|Line Numbers
  1. <form id="form1" runat="server">
  2.     <div>
  3.  
  4.            &nbsp;
  5.           <asp:GridView ID="GridView" AutoGenerateColumns="false"  runat="server" BackColor="Azure" BorderColor="#3366CC"
  6.         BorderStyle="None" BorderWidth="1px" CellPadding="4" Height="1px"
  7.         AllowPaging="True" PageSize ="5" OnPageIndexChanging="GridView_Paging"     style="left: 3px; top: 185px" CssClass="position" Width="466px"   OnRowCommand ="GridView_RowCommand" OnSelectedIndexChanged="GridView_SelectedIndexChanged">
  8.         <FooterStyle BackColor="#99CCCC" ForeColor="#003399" Font-Size="10px" />
  9.         <RowStyle BackColor="White" ForeColor="#003399" Font-Size="10px" Height="8px" />
  10.         <PagerStyle BackColor="#99CCCC" ForeColor="#8080FF" HorizontalAlign="Left" Font-Size="10px" Height="8px" />
  11.         <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" Font-Size="10px" />
  12.         <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" Font-Size="10px" />
  13.        <Columns>
  14.         <asp:TemplateField  >
  15.  
  16.         <HeaderTemplate >ORDER NO</HeaderTemplate>
  17.         <ItemTemplate>
  18.              <asp:LinkButton ID="link" runat ="server"  Text= '<%#DataBinder.Eval(Container.DataItem, "SL_NO")%>' CommandName ='<%#DataBinder.Eval(Container.DataItem, "RID")%>'  OnClick ="Link_Click"></asp:LinkButton>
  19.         </ItemTemplate>
  20.  
  21.         </asp:TemplateField>
  22.  
  23.          <asp:TemplateField >
  24.  
  25.         <HeaderTemplate >ID</HeaderTemplate>
  26.         <ItemTemplate>
  27.         <asp:Label ID="lb2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "LOC_ID")%>'></asp:Label>
  28.         </ItemTemplate>
  29.         </asp:TemplateField>
  30.  
  31.  
  32.  
  33.          <asp:TemplateField >
  34.  
  35.         <HeaderTemplate >NAME IN ENGLISH</HeaderTemplate>
  36.         <ItemTemplate>
  37.         <asp:Label ID="lb4" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "LOC_DE_EN")%>'></asp:Label>
  38.         </ItemTemplate>
  39.         </asp:TemplateField>
  40.         <asp:TemplateField >
  41.  
  42.         <HeaderTemplate >NAME IN ARABIC</HeaderTemplate>
  43.         <ItemTemplate>
  44.         <asp:Label ID="lb5" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "LOC_DE_AR")%>'></asp:Label>
  45.         </ItemTemplate>
  46.         </asp:TemplateField>
  47.         <asp:TemplateField >
  48.  
  49.         <HeaderTemplate >STATUS</HeaderTemplate>
  50.         <ItemTemplate>
  51.         <asp:Label ID="lb6" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "STATUS")%>'></asp:Label>
  52.         </ItemTemplate>
  53.         </asp:TemplateField>
  54.  
  55.  
  56.  
  57. </Columns>
  58.     </asp:GridView>
  59.     </div>
  60.     </form>
Sep 29 '10 #5
Frinavale
9,735 Expert Mod 8TB
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...according to the table the user is working with?

-Frinny
Sep 29 '10 #6
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.
Sep 29 '10 #7
Frinavale
9,735 Expert Mod 8TB
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
Sep 29 '10 #8
Can you give an example for changing Header Text of columns at run time?
Sep 30 '10 #9
Frinavale
9,735 Expert Mod 8TB
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)
Expand|Select|Wrap|Line Numbers
  1. Protected Sub MyGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowDataBound
  2.   If e.Row.RowType = DataControlRowType.Header Then
  3.     Dim link As LinkButton = Ctype(e.Row.FindControl("link"), LinkButton)
  4.     If link IsNot Nothing
  5.       ' do stuff with the "link" in the header
  6.     End If
  7.   End If
  8. End Sub
-Frinny
Sep 30 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: alsemgeest | last post by:
Hi, I'd like to change the properties of the default form design. Every form I need to change many settings. Especially the following properties (I try to translate them well from Dutch): * make...
5
by: Ram [MSFT] | last post by:
Hi All, I'm trying to programatically (using c#) read the file properties (Title, Summary, Author, Comments etc.... The stuff that shows up on the Summary tab when you see the properties of a...
3
by: cleo | last post by:
In VB6 my practice was to set control properties at Run Time rather than Design Time. I found setting property values in the code provided better documentation, since much of this was hidden - and...
0
by: Giorgio | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older...
2
by: rufpirat | last post by:
Hello I'm in the middle of trying to build an "AD phone book", and this being my first try at asp.net, I have a few questions that I hope some of you might be able to help with: 1. Is it...
0
by: Giovanni | last post by:
I was wondering if someone would be able to help me with the following: Simply put, I have built an ASP.NET 2.0 WebUserControl (UC1). UC1 contains: A GridView which is bound to an SQLDataSource...
39
by: Paul Mcilreavy | last post by:
Hi, i would like to get some view points on the use of properties within classes. My belief is that properties should generally be used to return values of private members. They should not do...
6
by: Greg | last post by:
Hello, I have a GridView bound to a custom object. I set the DataKeyNames property along with the column DataField properties at design time, and bind the GridView to my object at run-time. In...
1
by: Mark Olbert | last post by:
I have a GridView as a child control in a custom composite control which is stubornly refusing to databind at design time. I'm convinced I must be missing something about how the databinding process...
1
by: Mark Olbert | last post by:
Has anyone else noticed that the design-time support for databinding in custom controls in ASPNET2 sucks? At least for GridViews? So far I've spent going on two days trying to get the following...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.