简体   繁体   中英

how to prevent the DataGridView column increasing

I have a DataGridView. I am binding the DataGridBiew with a datasource as below...

  private void form_load(object sender, eventargs e)
  {
         var members = xyz.members(..
                         select new {
                            blah....
                               blahh...
                             });
         memberbindingsource.datasource = members
         membergridview1.datasource = memberbindingsource;

         // at here i have added button column to membergridview 1 
         // like this....
         DataGridViewButtonColumn column = new DataGridViewButtonColumn();
         membergridview1.Columns.Add(column);
         column.FlatStyle = FlatStyle.Standard;
         column.DefaultCellStyle.BackColor = Color.Green;
         column.Text = "ADD";
         column.HeaderText = "Add";
         column.UseColumnTextForButtonValue = true;
         column.Name = "btnadd";

this works fine at form load but somewhere I check the conditions in the combobox like this

            if(combobox1.text == "abc")
            {

                   then  i have to loaded the grid view again 
                    like this ...
                     var members = xyz.members(..
                         select new {
                            blah....
                               blahh...
                             });
                         memberbindingsource.datasource = members
                         membergridview1.datasource = memberbindingsource;

     at here i have added button column to membergridview 1 
        like this....
        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
       membergridview1.Columns.Add(column);
        column.FlatStyle = FlatStyle.Standard;
        column.DefaultCellStyle.BackColor = Color.Green;
        column.Text = "ADD";
        column.HeaderText = "Add";
        column.UseColumnTextForButtonValue = true;
        column.Name = "btnadd";

      }

when is first select the combobox1 text "abc" the datagridview was showing the button column thats fine ...

when I check the first conditon ( like this combobox1.text == "abc" ) the DataGridView displaying details ok but if I check again the same condition it will again added the button column.

I want to show only one button column with members data when I check the same condition again and again.

How can I prevent this - not increasing the button column again and again?

This is because everytime your page loads you are adding this column. So no matter this is a fresh call or a post back call this column is added. To get this work proper enclose the code snippet that adds the column in following if condition

if (!IsPostBack)
    {
        //add your column add code snippet here 

    }

so your code will look something like this

if (!IsPostBack)
    {
        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
       membergridview1.Columns.Add(column);
        column.FlatStyle = FlatStyle.Standard;
        column.DefaultCellStyle.BackColor = Color.Green;
        column.Text = "ADD";
        column.HeaderText = "Add";
        column.UseColumnTextForButtonValue = true;
        column.Name = "btnadd";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM