简体   繁体   中英

DataGridView Button Column for Deleting

I am having this problem with my datagridview where the first time it runs the delete button is on the last column, but everytime when i click on delete or adds, and call this printdataview() function.. the button is on the first column.

This function is called initially on Form1(), then calls everytime, I delete a record or add a record. I am using xml for storing data and add and remove records according, this printdataview() just simply refreshes the data on it.. and somehow it messes up, even the column length is messed the first time the datagridview was initialized and after.

Thanks Appreciate the feedback.

 private void PrintDataView()
    {
        // clears the old data and repopulate it.
        C_DB.DataSource = null;

        XmlReader xmlFile;
        xmlFile = XmlReader.Create(filename, new XmlReaderSettings());

            DataSet ds = new DataSet();
            ds.ReadXml(xmlFile);

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0){

            DataView dv = new DataView(ds.Tables[0]);


                // first adds all rows after sorting today's list into datagridview
                string Search = DateTime.Today.ToShortDateString();
                dv.RowFilter = "DateTime LIKE '%" + Search + "%'";
                dv.Sort = "DateTime ASC";

                C_DB.DataSource = dv;


                // then add the delete button if there is more than one row

                if (dv.Count > 0 && C_DB.ColumnCount != 7 && C_DB.RowCount > 0)
                {
                    // add button
                    DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
                    C_DB.Columns.Add(btn);
                    btn.HeaderText = "Delete Row";
                    btn.Text = "Delete";
                    btn.Name = "btn";
                    btn.UseColumnTextForButtonValue = true;
                }


                // This scrolls to bottom
                if (C_DB.RowCount > 10)
                {
                    C_DB.FirstDisplayedScrollingRowIndex = C_DB.RowCount - 1;
                }


            }
            else
            {
                C_ErrorMessage.Text = "No Data Found";

            }
            C_DB.Refresh();
            xmlFile.Close();

    }

Nvm, I found out what's wrong.

Instead of

// clears the old data and repopulate it.

C_DB.DataSource = null;

// I changed it to

C_DB.Columns.Clear();

Apparently nulling the datasource doesn't empty the structure from previously.

This also fixed my column widths.. which I set later

Thanks.

Note this is not a complete solution.

The problem you have that the Delete button appears at the end of the columns list the first time only and then changes position to first, is because when you added the button by executing the method PrintDataView() , you did not specify its position in the DataGridView. Use something like this:

 //Your code
 C_DB.Columns.Add(btn);
 btn.HeaderText = "Delete Row";
 btn.Text = "Delete";
 btn.Name = "btn";
 btn.UseColumnTextForButtonValue = true;
 //Set the desired button position here
 C_DB.Columns[btn.Name].DisplayIndex = 5; //Whatever value you want starting from zero

For more info. see: Full example on MS Site

Now, you can adjust each column width individually or set a property to max the width of all columns based on the contents as explained here:

DGV Column Size

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