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. C#
  4. ComboBox in DataGrid

ComboBox in DataGrid

Scheduled Pinned Locked Moved C#
csshelp
7 Posts 3 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.
  • P Offline
    P Offline
    pahluwalia
    wrote on last edited by
    #1

    Hi, I want ComboBoxes displayed in all columns of datgrid, always visible. And there are a lot of things in DataGrid that are difficult to resolve. For ex, I always see one extra row at the bottom even when I have added the rows through DataTable and the DataTable.Row.Count is correct but Grid.VisibleRowCount is 1 greater. Also, I don't want the column with readonly=true to be selected, whereas when I click on that column the background of that row becomes gray ! Any help would be highly appreciated :((! Thanks.

    S A 2 Replies Last reply
    0
    • P pahluwalia

      Hi, I want ComboBoxes displayed in all columns of datgrid, always visible. And there are a lot of things in DataGrid that are difficult to resolve. For ex, I always see one extra row at the bottom even when I have added the rows through DataTable and the DataTable.Row.Count is correct but Grid.VisibleRowCount is 1 greater. Also, I don't want the column with readonly=true to be selected, whereas when I click on that column the background of that row becomes gray ! Any help would be highly appreciated :((! Thanks.

      S Offline
      S Offline
      sumeat
      wrote on last edited by
      #2

      To disallow users from clicking or selected a particular column, you will have to use custom table style for your data grid. In this custom table style you will have to add custom data column styles - DataGridTextBoxColumn. Derive a class from DataGridTextBoxColumn and in that override the Edit method and in that method do nothing. public class MyTextBoxColumn : DataGridTextBoxColumn { public MyTextBoxColumn() { // // TODO: Add constructor logic here // } protected override void Edit(...) { if (!ReadOnly) { base.Edit(..); // call base class } //don't call baseclass, so no editing.... } } This should take care of it! Hope this helps. Suhas

      P 1 Reply Last reply
      0
      • P pahluwalia

        Hi, I want ComboBoxes displayed in all columns of datgrid, always visible. And there are a lot of things in DataGrid that are difficult to resolve. For ex, I always see one extra row at the bottom even when I have added the rows through DataTable and the DataTable.Row.Count is correct but Grid.VisibleRowCount is 1 greater. Also, I don't want the column with readonly=true to be selected, whereas when I click on that column the background of that row becomes gray ! Any help would be highly appreciated :((! Thanks.

        A Offline
        A Offline
        A Wegierski
        wrote on last edited by
        #3

        To see ComboBox in DataGrid always - try to override Paint method in suhass'es example. To avoid extra row set DataGrid ListManager protected property: (DataView)(myDataGrid.ListManager.List).AllowNew=false; Hi, AW

        P 1 Reply Last reply
        0
        • S sumeat

          To disallow users from clicking or selected a particular column, you will have to use custom table style for your data grid. In this custom table style you will have to add custom data column styles - DataGridTextBoxColumn. Derive a class from DataGridTextBoxColumn and in that override the Edit method and in that method do nothing. public class MyTextBoxColumn : DataGridTextBoxColumn { public MyTextBoxColumn() { // // TODO: Add constructor logic here // } protected override void Edit(...) { if (!ReadOnly) { base.Edit(..); // call base class } //don't call baseclass, so no editing.... } } This should take care of it! Hope this helps. Suhas

          P Offline
          P Offline
          pahluwalia
          wrote on last edited by
          #4

          Hi Suhas, I tried what you said but it's not working the way I implemented it. I'll explain what I did. //Create a custom Column Style for the 1st Col: Questions CurrencyManager cm = (CurrencyManager)this.BindingContext[dtTable]; PropertyDescriptor pd = cm.GetItemProperties()[0];//Get Prop for the 1st col MyControls.MyDataGridTextBoxColumn myColStyle = new MyControls.MyDataGridTextBoxColumn(pd); newGrid[indexPage].TableStyles.Add(dgdtblStyle); newGrid[indexPage].TableStyles[0].GridColumnStyles.Add(myColStyle); And to create the MyControl Class: public class MyDataGridTextBoxColumn: DataGridTextBoxColumn { public MyDataGridTextBoxColumn(PropertyDescriptor pd) : base(pd) { } protected override void Edit(CurrencyManager cm, int rowNum, Rectangle bounds, bool readOnly) { MessageBox.Show("in Edit"); if (!readOnly) base.Edit(cm, rowNum, bounds, readOnly); } } But that MessageBox is bever clicked & nothing diff happens when I try to edit the 0th Col ! What do you think I am doing wrong ? Thanks. Paul

          P S 2 Replies Last reply
          0
          • P pahluwalia

            Hi Suhas, I tried what you said but it's not working the way I implemented it. I'll explain what I did. //Create a custom Column Style for the 1st Col: Questions CurrencyManager cm = (CurrencyManager)this.BindingContext[dtTable]; PropertyDescriptor pd = cm.GetItemProperties()[0];//Get Prop for the 1st col MyControls.MyDataGridTextBoxColumn myColStyle = new MyControls.MyDataGridTextBoxColumn(pd); newGrid[indexPage].TableStyles.Add(dgdtblStyle); newGrid[indexPage].TableStyles[0].GridColumnStyles.Add(myColStyle); And to create the MyControl Class: public class MyDataGridTextBoxColumn: DataGridTextBoxColumn { public MyDataGridTextBoxColumn(PropertyDescriptor pd) : base(pd) { } protected override void Edit(CurrencyManager cm, int rowNum, Rectangle bounds, bool readOnly) { MessageBox.Show("in Edit"); if (!readOnly) base.Edit(cm, rowNum, bounds, readOnly); } } But that MessageBox is bever clicked & nothing diff happens when I try to edit the 0th Col ! What do you think I am doing wrong ? Thanks. Paul

            P Offline
            P Offline
            pahluwalia
            wrote on last edited by
            #5

            Hi, I put a label on top of that column in the data grid & now u cannot edit the text in that column. But if u click on any editable control like combo box in the datagrid, or even at any place in the grid where there are no rows, the first row gets selected ... What's happening !?! I was able to remove that extra row with AllowNew property set to false. Thanks for all ur help. But I think we need a much stronger DatagRid than what we have right now ! Paul

            1 Reply Last reply
            0
            • A A Wegierski

              To see ComboBox in DataGrid always - try to override Paint method in suhass'es example. To avoid extra row set DataGrid ListManager protected property: (DataView)(myDataGrid.ListManager.List).AllowNew=false; Hi, AW

              P Offline
              P Offline
              pahluwalia
              wrote on last edited by
              #6

              I was able to avoid that extra row the way you said. Is there a way to have Vertical Scrolling on in a DataGrid ? Thanks

              1 Reply Last reply
              0
              • P pahluwalia

                Hi Suhas, I tried what you said but it's not working the way I implemented it. I'll explain what I did. //Create a custom Column Style for the 1st Col: Questions CurrencyManager cm = (CurrencyManager)this.BindingContext[dtTable]; PropertyDescriptor pd = cm.GetItemProperties()[0];//Get Prop for the 1st col MyControls.MyDataGridTextBoxColumn myColStyle = new MyControls.MyDataGridTextBoxColumn(pd); newGrid[indexPage].TableStyles.Add(dgdtblStyle); newGrid[indexPage].TableStyles[0].GridColumnStyles.Add(myColStyle); And to create the MyControl Class: public class MyDataGridTextBoxColumn: DataGridTextBoxColumn { public MyDataGridTextBoxColumn(PropertyDescriptor pd) : base(pd) { } protected override void Edit(CurrencyManager cm, int rowNum, Rectangle bounds, bool readOnly) { MessageBox.Show("in Edit"); if (!readOnly) base.Edit(cm, rowNum, bounds, readOnly); } } But that MessageBox is bever clicked & nothing diff happens when I try to edit the 0th Col ! What do you think I am doing wrong ? Thanks. Paul

                S Offline
                S Offline
                sumeat
                wrote on last edited by
                #7

                pahluwalia@hotmail.com wrote: newGrid[indexPage].TableStyles.Add(dgdtblStyle); newGrid[indexPage].TableStyles[0].GridColumnStyles.Add(myColStyle); Add custom column styles to the custom table style AND then add the custom table style to the datagrid's table style collection. If you do the reverse, the way you have done, the data grid will not use your column styles

                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