locked
Grid RRS feed

  • Question

  • Hi friends,

    I have a requirment i.e i have to create grid in c# class and i need to declare the coloumns.every thing is class only.any one can heppppppp.plzzzzzzz

    I will use this class in a sharepoint lis items..........
    Wednesday, December 2, 2009 8:14 AM

Answers

  • Hi Prasad,
     
    binding  a array to the gridview will not give you much control on the columns you want to create.

    here is a solution

    public enum ControlType
    { 
        TextBox,
        CheckBox,
        DropDown,
    }
    
    //Create a custom templatefield
    
    public class CustomTemplate :  ITemplate
    {
        ControlType _type;
    
        public CustomTemplate(ControlType type)
        {
            _type = type;
        }
        #region ITemplate Members
    
        public void InstantiateIn(Control container)
        {
            switch(_type)
            {
                case ControlType.TextBox:
                TextBox t = new TextBox();
                t.ID = "tt";
    
                container.Controls.Add(t);
                break;
    
                case ControlType.CheckBox:
                CheckBox c = new CheckBox();
                c.ID = "cc";
                container.Controls.Add(c);
                break;
                //other controls you want to add
            }
        }
    
        #endregion
    }
    
    //Create Custom GridView
    public class CustomGridView:GridView
    {
        public CustomGridView()
    	{
            this.Columns.Add(new TemplateField() { HeaderText = "test", ItemTemplate = new CustomTemplate(ControlType.TextBox) });
            this.Columns.Add(new BoundField() { HeaderText = "Test2",DataField="col1",Visible=false }); //Set to false to hold bounddata
            this.Columns.Add(new TemplateField() { HeaderText = "Test3", ItemTemplate = new CustomTemplate(ControlType.CheckBox) });
            ////this.Columns.Add(new BoundField() { HeaderText = "Test3" });
            this.AutoGenerateColumns = false;
    	}
    
    
    
    
    }
    
    //Create a class that can be data bound
    public class testobj
    {
        public string col1 { get; set; }
    
    
    }


    and the in your code

    new EditFormSection()
                                            {
                                                Title="Grid",
                                                Collapsible=true,
                                                Description="Grid values",
                                                Controls =
                                                {
                                                    new EditFormControl ()
                                                    {
                                                       LabelText="XContent values",
                                                       Controls=
                                                        {
                                                            (_grid =new CustomGridView()
                                                            {
                                                                ID="Grid",
                                                                CssClass=MsCssClass.Input
                                                            }),
                                                          }
                                                        }
                                                     }
                                                  },


    for databinding
     protected override void OnInit(System.EventArgs e)
            {
                base.OnInit(e);
    
                 //create a list
                              
                  List<testobj> objs = new List<testobj>();
    
                objs.Add(new testobj() { col1 = "abc" });
    
               objs.Add(new testobj() { col1 = "abc" });
    
                  objs.Add(new testobj() { col1 = "abc" });
    
    
                 
                if (_grid != null)
                {
                    _grid.DataSource = objs;
                 
                        _grid.RowDataBound += new GridViewRowEventHandler(cs_RowDataBound);
    
    
                    _grid.DataBind();
            
                }
    


     
    //Handel the rowbound event

        void cs_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox tt = e.Row.FindControl("tt") as TextBox;
                testobj t = e.Row.DataItem as testobj;
    if(t!=null && tt!=null)

    tt.Text = t.col1; } }
    for retreving the data fro the grid just loop through the rows and find the controls by their Id's
    let me know if this solves your issue
    • Proposed as answer by GLEN_FERNANDES Thursday, December 10, 2009 11:41 AM
    • Marked as answer by eryang Friday, December 11, 2009 2:37 AM
    Thursday, December 10, 2009 4:28 AM

All replies

  • Hi Prasad,
       
       You can use inheritance to do this .

       you have to create a component which inherits form the the grid

       here is a small example . i have used DataGridView from System.Windows.Forms namespace.
       since you want to use it in sharepoint  you will have to a ASP.Net grid to do this.

      but the fundamentals are the same.

    namespace WindowsFormsApplication1
    {
        public class CustomGrid : DataGridView
        {
            public CustomGrid():base()
            {
                this.Columns.Add("Test", "Test");
            }
        }
    }
    Glen
    Thursday, December 3, 2009 1:39 AM
  • Hi Prasad,

    Please give more info.

    Do you want to create Grid in ASP.NET or Windows or WPF?

    Regards,
    Jai
    Thursday, December 3, 2009 1:39 AM
  • Hi GLEN_FERNANDES,

    Good Day,
    I need to create controls dynamycally in the class library.the which follows like

    hi friends ,



    i have a requirment i.e i need to create grid with 4 coloumns in this grid

     first one is text coloumn

    second id check box coloumn

    third id is dropdown

    4th one is combobox



    these r all create  dynamically in c# class library.this is example code.........

    new EditFormSection()
                                            {
                                                Title="Grid",
                                                Collapsible=true,
                                                Description="Grid values",
                                                Controls =
                                                {
                                                    new EditFormControl ()
                                                    {
                                                       LabelText="XContent values",
                                                       Controls=
                                                        {
                                                            (_grid =new GridView()
                                                            {
                                                                ID="Grid",
                                                                CssClass=MsCssClass.Input
                                                            }),
                                                          }
                                                        }
                                                     }
                                                  },





    and this is bindingg data to grid

     protected override void OnInit(System.EventArgs e)
            {
                base.OnInit(e);


                if (_grid != null)
                {
                    _grid.DataSource = new string[] { "abc", "abc"};
                    _grid.DataBind();
           
                }

    .but ineed multiple coloumns.please any one help meeeeeeeeee.
    Above abc ic first coloumn and 2nd coloumn is check boxes,third is dropdown,4th is combobox,,,,,,,,,,
    please helpppppppppppppppppppppp
    Thursday, December 3, 2009 6:30 AM
  • Hi,Mallesh Babu,

    Good Day,
    I need to create controls dynamycally in the class library.the which follows like

    hi friends ,



    i have a requirment i.e i need to create grid with 4 coloumns in this grid

     first one is text coloumn

    second id check box coloumn

    third id is dropdown

    4th one is combobox



    these r all create  dynamically in c# class library.this is example code.........

    new EditFormSection()
                                            {
                                                Title="Grid",
                                                Collapsible=true,
                                                Description="Grid values",
                                                Controls =
                                                {
                                                    new EditFormControl ()
                                                    {
                                                       LabelText="XContent values",
                                                       Controls=
                                                        {
                                                            (_grid =new GridView()
                                                            {
                                                                ID="Grid",
                                                                CssClass=MsCssClass.Input
                                                            }),
                                                          }
                                                        }
                                                     }
                                                  },





    and this is bindingg data to grid

     protected override void OnInit(System.EventArgs e)
            {
                base.OnInit(e);


                if (_grid != null)
                {
                    _grid.DataSource = new string[] { "abc", "abc"};
                    _grid.DataBind();
           
                }

    .but ineed multiple coloumns.please any one help meeeeeeeeee.
    Above abc ic first coloumn and 2nd coloumn is check boxes,third is dropdown,4th is combobox,,,,,,,,,,
    please helpppppppppppppppppppppp

    Thursday, December 3, 2009 6:31 AM
  • Hi,

    Could you please elaborate more clearly about what kind of application you are developing? ASP.NET, WinForm or others.

     

    Thanks,

    Eric


    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Wednesday, December 9, 2009 3:01 AM
  • Hi eryang,

    Good day

    I am developing sharepoint portal.so we are using wss object model.every thing thing is in asp.net.
    above i posted my code also...............
    Wednesday, December 9, 2009 7:30 AM
  • Hi Prasad,
     
    binding  a array to the gridview will not give you much control on the columns you want to create.

    here is a solution

    public enum ControlType
    { 
        TextBox,
        CheckBox,
        DropDown,
    }
    
    //Create a custom templatefield
    
    public class CustomTemplate :  ITemplate
    {
        ControlType _type;
    
        public CustomTemplate(ControlType type)
        {
            _type = type;
        }
        #region ITemplate Members
    
        public void InstantiateIn(Control container)
        {
            switch(_type)
            {
                case ControlType.TextBox:
                TextBox t = new TextBox();
                t.ID = "tt";
    
                container.Controls.Add(t);
                break;
    
                case ControlType.CheckBox:
                CheckBox c = new CheckBox();
                c.ID = "cc";
                container.Controls.Add(c);
                break;
                //other controls you want to add
            }
        }
    
        #endregion
    }
    
    //Create Custom GridView
    public class CustomGridView:GridView
    {
        public CustomGridView()
    	{
            this.Columns.Add(new TemplateField() { HeaderText = "test", ItemTemplate = new CustomTemplate(ControlType.TextBox) });
            this.Columns.Add(new BoundField() { HeaderText = "Test2",DataField="col1",Visible=false }); //Set to false to hold bounddata
            this.Columns.Add(new TemplateField() { HeaderText = "Test3", ItemTemplate = new CustomTemplate(ControlType.CheckBox) });
            ////this.Columns.Add(new BoundField() { HeaderText = "Test3" });
            this.AutoGenerateColumns = false;
    	}
    
    
    
    
    }
    
    //Create a class that can be data bound
    public class testobj
    {
        public string col1 { get; set; }
    
    
    }


    and the in your code

    new EditFormSection()
                                            {
                                                Title="Grid",
                                                Collapsible=true,
                                                Description="Grid values",
                                                Controls =
                                                {
                                                    new EditFormControl ()
                                                    {
                                                       LabelText="XContent values",
                                                       Controls=
                                                        {
                                                            (_grid =new CustomGridView()
                                                            {
                                                                ID="Grid",
                                                                CssClass=MsCssClass.Input
                                                            }),
                                                          }
                                                        }
                                                     }
                                                  },


    for databinding
     protected override void OnInit(System.EventArgs e)
            {
                base.OnInit(e);
    
                 //create a list
                              
                  List<testobj> objs = new List<testobj>();
    
                objs.Add(new testobj() { col1 = "abc" });
    
               objs.Add(new testobj() { col1 = "abc" });
    
                  objs.Add(new testobj() { col1 = "abc" });
    
    
                 
                if (_grid != null)
                {
                    _grid.DataSource = objs;
                 
                        _grid.RowDataBound += new GridViewRowEventHandler(cs_RowDataBound);
    
    
                    _grid.DataBind();
            
                }
    


     
    //Handel the rowbound event

        void cs_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox tt = e.Row.FindControl("tt") as TextBox;
                testobj t = e.Row.DataItem as testobj;
    if(t!=null && tt!=null)

    tt.Text = t.col1; } }
    for retreving the data fro the grid just loop through the rows and find the controls by their Id's
    let me know if this solves your issue
    • Proposed as answer by GLEN_FERNANDES Thursday, December 10, 2009 11:41 AM
    • Marked as answer by eryang Friday, December 11, 2009 2:37 AM
    Thursday, December 10, 2009 4:28 AM
  • Hi GLEN_FERNANDES,

    Thanku very much ,

    one more doubt i.e i have a number of items  binded to gridview.if client wants to change items order i was put one dropdown with iems id.when client change one item order automatically athor controls will change their respective order.is their any scope.

    this is the example:when we see task list settings we can observe this example.
    Could u please help meeeeeeeeeee


    thanks
     prasad
    Thursday, December 10, 2009 7:48 AM
  • Hi,

    Glad to see that the first question has been resolved, for ASP.NET related questions, you may get quicker and better support at ASP.NET forum where ASP experts live in.

     

    Thanks,

    Eric


    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Friday, December 11, 2009 2:37 AM