Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. how to change DataGrid specific column alignment to Center in C# programmatically

how to change DataGrid specific column alignment to Center in C# programmatically

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpcssdatabasehelptutorial
18 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Change the alignment AFTER binding - the datageridview does not have the columns at the point where you are accessing them. If it did, the index would not be out of range.

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

    V Offline
    V Offline
    ven753
    wrote on last edited by
    #5

    I added after binding then also its giving same error. dg1.DataSource = ds1.Tables["tblanni"].DefaultView; dg1.DataBind(); dg1.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Center; What to do. Please reply me.

    L 1 Reply Last reply
    0
    • V ven753

      I added after binding then also its giving same error. dg1.DataSource = ds1.Tables["tblanni"].DefaultView; dg1.DataBind(); dg1.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Center; What to do. Please reply me.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #6

      Is this a WinForms datagrid (prolly not), an ASP.NET grid or a WPF-grid? In WinForms, life is simple;

      namespace WindowsFormsApplication1
      {
      public partial class Form1 : Form
      {
      class Test
      {
      public string Item1 { get; set; }
      public string Item2 { get; set; }
      public string Item3 { get; set; }
      }

          DataGridView grid;
          public Form1()
          {
              List data = new List();
              data.Add(new Test() { Item1 = "bla", Item2 = "foos", Item3 = "bar" });
      
              InitializeComponent();
              grid = new DataGridView() { Dock = DockStyle.Fill };
      
              grid.DataSource = data;
      
              Controls.Add(grid);
      
              MessageBox.Show(string.Format("There are {0} columns in this grid", grid.Columns.Count));
          }
      
          protected override void OnShown(EventArgs e)
          {
              base.OnShown(e);
              grid.Columns\[0\].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
          }
      }
      

      }

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      V 1 Reply Last reply
      0
      • L Lost User

        Is this a WinForms datagrid (prolly not), an ASP.NET grid or a WPF-grid? In WinForms, life is simple;

        namespace WindowsFormsApplication1
        {
        public partial class Form1 : Form
        {
        class Test
        {
        public string Item1 { get; set; }
        public string Item2 { get; set; }
        public string Item3 { get; set; }
        }

            DataGridView grid;
            public Form1()
            {
                List data = new List();
                data.Add(new Test() { Item1 = "bla", Item2 = "foos", Item3 = "bar" });
        
                InitializeComponent();
                grid = new DataGridView() { Dock = DockStyle.Fill };
        
                grid.DataSource = data;
        
                Controls.Add(grid);
        
                MessageBox.Show(string.Format("There are {0} columns in this grid", grid.Columns.Count));
            }
        
            protected override void OnShown(EventArgs e)
            {
                base.OnShown(e);
                grid.Columns\[0\].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }
        }
        

        }

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        V Offline
        V Offline
        ven753
        wrote on last edited by
        #7

        No i am creating in windows service application. There i am using datagrid through service cs file i am creating dataset DataGrid dg1 = new DataGrid(); dg1.ItemStyle.HorizontalAlign = HorizontalAlign.Center; this will work for all the column items and align will become center. but i need to do for particular column not working dg1.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Center; How to achieve this and it is nor datagridview only DataGrid. Please reply me.

        P D 2 Replies Last reply
        0
        • V ven753

          No i am creating in windows service application. There i am using datagrid through service cs file i am creating dataset DataGrid dg1 = new DataGrid(); dg1.ItemStyle.HorizontalAlign = HorizontalAlign.Center; this will work for all the column items and align will become center. but i need to do for particular column not working dg1.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Center; How to achieve this and it is nor datagridview only DataGrid. Please reply me.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #8

          You're creating a UI element inside a Windows Service? Why? A service has no UI, so what are you trying to accomplish here?

          V 1 Reply Last reply
          0
          • P Pete OHanlon

            You're creating a UI element inside a Windows Service? Why? A service has no UI, so what are you trying to accomplish here?

            V Offline
            V Offline
            ven753
            wrote on last edited by
            #9

            We can use dataset and result is coming and displaying 4 columns. Only i need to do particular column alignment. But its not working.

            P 1 Reply Last reply
            0
            • V ven753

              We can use dataset and result is coming and displaying 4 columns. Only i need to do particular column alignment. But its not working.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #10

              You haven't answered my question. Why are you trying to do this inside a service? A service should have no user interface.

              V 1 Reply Last reply
              0
              • V ven753

                No i am creating in windows service application. There i am using datagrid through service cs file i am creating dataset DataGrid dg1 = new DataGrid(); dg1.ItemStyle.HorizontalAlign = HorizontalAlign.Center; this will work for all the column items and align will become center. but i need to do for particular column not working dg1.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Center; How to achieve this and it is nor datagridview only DataGrid. Please reply me.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #11

                Ummm, you've got a HUGE problem. Windows Services cannot display a UI. Starting with Windows Vista, services that attempt to display a UI only show their interface on a separate desktop from the user that's logged in. The user gets a notification that a service has put up a UI window and asked if they want to switch desktops to see it. UI restrictions on services have now gotten to the point where a service cannot show a UI at all. This "transition" period comes from the days on Windows XP where a service could show a UI. This is a security risk so Microsoft has removed the ability to do this from Windows.

                A guide to posting questions on CodeProject

                How to debug small programs
                Dave Kreskowiak

                V 1 Reply Last reply
                0
                • V ven753

                  Hi i have created DataGrid and i need to align the specific column (4th column i.e index 3) to Center in C# programmatically. I tried as below dg1.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Center; but giving error as follows Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index even if i give index 0 also same error How to achieve this. If anybody knows reply me. Thanks in advance.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #12

                  Next to the "no UI" issue, there's also the point the a DataGrid[^] doesn't have a columns-property. Yes, it is faster in displaying data than a DataGridView, but it doesn't have as much features built in.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  V 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    You haven't answered my question. Why are you trying to do this inside a service? A service should have no user interface.

                    V Offline
                    V Offline
                    ven753
                    wrote on last edited by
                    #13

                    Ya but i have created one html file and for that i am sending this dataset after rendering as StringWriter stringWriter1 = new StringWriter(); HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWriter1); i am trying read html file and after reading just rendering the control as shown below dg1.RenderControl(htmlWrite1); i am getting html file result and that 4 column grid. Only i need to make particular column alignment. How to do this.

                    P 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      Ummm, you've got a HUGE problem. Windows Services cannot display a UI. Starting with Windows Vista, services that attempt to display a UI only show their interface on a separate desktop from the user that's logged in. The user gets a notification that a service has put up a UI window and asked if they want to switch desktops to see it. UI restrictions on services have now gotten to the point where a service cannot show a UI at all. This "transition" period comes from the days on Windows XP where a service could show a UI. This is a security risk so Microsoft has removed the ability to do this from Windows.

                      A guide to posting questions on CodeProject

                      How to debug small programs
                      Dave Kreskowiak

                      V Offline
                      V Offline
                      ven753
                      wrote on last edited by
                      #14

                      Ya but i have created one html file and for that i am sending this dataset after rendering as StringWriter stringWriter1 = new StringWriter(); HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWriter1); i am trying read html file and after reading just rendering the control as shown below dg1.RenderControl(htmlWrite1); i am getting html file result and that 4 column grid. Only i need to make particular column alignment. How to do this.

                      D 1 Reply Last reply
                      0
                      • L Lost User

                        Next to the "no UI" issue, there's also the point the a DataGrid[^] doesn't have a columns-property. Yes, it is faster in displaying data than a DataGridView, but it doesn't have as much features built in.

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                        V Offline
                        V Offline
                        ven753
                        wrote on last edited by
                        #15

                        But through coding if i type DataGrid.Columns[] Columns property it will show. But after adding below code it is not working dg1.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Center; What to do. Please reply me.

                        L 1 Reply Last reply
                        0
                        • V ven753

                          Ya but i have created one html file and for that i am sending this dataset after rendering as StringWriter stringWriter1 = new StringWriter(); HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWriter1); i am trying read html file and after reading just rendering the control as shown below dg1.RenderControl(htmlWrite1); i am getting html file result and that 4 column grid. Only i need to make particular column alignment. How to do this.

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #16

                          If you're just trying to write out to a file as HTML, then why are you trying to write it out this way? A simpler method would be to save your DataSet out as XML and use a page that XPath to read and display the content.

                          1 Reply Last reply
                          0
                          • V ven753

                            Ya but i have created one html file and for that i am sending this dataset after rendering as StringWriter stringWriter1 = new StringWriter(); HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWriter1); i am trying read html file and after reading just rendering the control as shown below dg1.RenderControl(htmlWrite1); i am getting html file result and that 4 column grid. Only i need to make particular column alignment. How to do this.

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #17

                            What does this have to do with writing a Windows Service application?

                            A guide to posting questions on CodeProject

                            How to debug small programs
                            Dave Kreskowiak

                            1 Reply Last reply
                            0
                            • V ven753

                              But through coding if i type DataGrid.Columns[] Columns property it will show. But after adding below code it is not working dg1.Columns[3].ItemStyle.HorizontalAlign = HorizontalAlign.Center; What to do. Please reply me.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #18

                              ven753 wrote:

                              Columns property it will show.

                              Only if you're referencing a DataGridView, and that's a different control. Either you switch to the DataGridView, or you adjust the requirement of needing the alignment.

                              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups