Bonsoir,

Dans le GridView que j'affiche, j'aimerais y ajouter des boutons pour l'édition, et éventuellement ajouter une nouvelle ligne (si c'est possible). Sauf que je ne vois pas comment l'implémenter dans mon code actuel.

Mon GirdView est généré dynamiquement, donc je pense qu'il faut rajouter quelques détails pour savoir si c'est un bouton ou si c'est des données prisent du DataSet. Si quelqu'un pouvait me donne une petite idée ou le chemin à suivre, merci bien! : )

Voici mon code.

Ci-dessous, la classe qui génère les colonnes:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
public class DynamicGridViewTextTemplate : ITemplate
    {
        string _ColName;
        DataControlRowType _rowType;
        int _Count;
 
        public DynamicGridViewTextTemplate(string ColName, DataControlRowType RowType)
        {
            _ColName = ColName;
            _rowType = RowType;
        }
 
        public DynamicGridViewTextTemplate(DataControlRowType RowType, int ArticleCount)
        {
            _rowType = RowType;
            _Count = ArticleCount;
        }
 
        public void InstantiateIn(System.Web.UI.Control container)
        {
            switch (_rowType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.Text = "<b>" + _ColName + "</b>";
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    Label lbl = new Label();
                    lbl.DataBinding += new EventHandler(this.lbl_DataBind);
                    container.Controls.Add(lbl);
                    break;
                default:
                    break;
            }
        }
 
        private void lbl_DataBind(Object sender, EventArgs e)
        {
            Label lbl = (Label)sender;
            GridViewRow row = (GridViewRow)lbl.NamingContainer;
            lbl.Text = DataBinder.Eval(row.DataItem, _ColName).ToString();
        }
    }
Ma fonction qui prend en paramètre, le DataSet avec les données, le nom et l'ID à afficher comme header du GridView:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
private void createAndDisplayGridView(DataSet questions, String categoryName, String categoryID)
        {
            for (int j = 0; j < questions.Tables.Count; j++)
            {
                if (questions.Tables[j].Rows.Count > 0)
                {
                    GridView gvDynamic = new GridView();
                    gvDynamic.Width = Unit.Pixel(900);
                    //gvDynamic.HorizontalAlign = HorizontalAlign.Center;
                    gvDynamic.BorderWidth = Unit.Pixel(1);
                    gvDynamic.Caption = "<div id=\"nifty\" class=\"PostCategory\"> <b class=\"rtop\"><b class=\"r1\"></b><b class=\"r2\"></b><b class=\"r3\"></b><b class=\"r4\"></b></b>" + categoryID + " - " + categoryName + " <b class=\"rbottom\"><b class=\"r4\"></b><b class=\"r3\"></b><b class=\"r2\"></b><b class=\"r1\"></b></b></div>";
                    gvDynamic.AutoGenerateColumns = false;
                    gvDynamic.ShowFooter = false;
                    TemplateField tf = null;
 
                    tf = new TemplateField();
                    tf.HeaderTemplate = new DynamicGridViewTextTemplate("ID", DataControlRowType.Header);
                    tf.ItemTemplate = new DynamicGridViewTextTemplate("ID", DataControlRowType.DataRow);
 
                    gvDynamic.Columns.Add(tf);
 
                    tf = new TemplateField();
                    tf.HeaderTemplate = new DynamicGridViewTextTemplate("Question(s)", DataControlRowType.Header);
                    tf.ItemTemplate = new DynamicGridViewTextTemplate("child", DataControlRowType.DataRow);
                    gvDynamic.Columns.Add(tf);
 
                    tf = new TemplateField();
                    tf.HeaderTemplate = new DynamicGridViewTextTemplate("Pondération", DataControlRowType.Header);
                    tf.ItemTemplate = new DynamicGridViewTextTemplate("weighting", DataControlRowType.DataRow);
                    gvDynamic.Columns.Add(tf);
 
                    tf = new TemplateField();
                    tf.HeaderTemplate = new DynamicGridViewTextTemplate("Question par Defaut?", DataControlRowType.Header);
                    tf.ItemTemplate = new DynamicGridViewTextTemplate("isDefaultQuestion", DataControlRowType.DataRow);
                    gvDynamic.Columns.Add(tf);
 
                    gvDynamic.DataSource = questions.Tables[j];
                    gvDynamic.DataBind();
                    plHDynGridView.Controls.Add(gvDynamic);
                }
            }
        }//createAndDisplayGridView
Je pense que je ne suis pas loin du résultat, mais je ne sais pas trop comment faire :/

Merci bien pour votre et bonne soirée!

L.