C# DataGridView 更改類型 重繪


DataGridView   更改類型

需要用到重繪

            DataGridViewTextBoxColumn aa01 = new DataGridViewTextBoxColumn();
            aa00.DataPropertyName = "題目"; //綁定數據源的名稱
            aa00.HeaderText = "題目00000";  //顯示的名稱
            aa00.Name = "題目"; //列的名稱
            dataGridView1.Columns.Insert(1, aa01);

//綁定重繪事件

 
         
 private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            try
            {
                int index = dgv.Columns["狀態"].Index;  //獲取列的索引值
                if (e.ColumnIndex >= index) // ColumnIndex 正在格式化單元格的索引
                {
                    if (e.Value.GetType().Name == "Boolean")
                    {
                        if ((bool)e.Value)
                        {
                            e.Value = "";
                        }
                        else
                        {
                            e.Value = string.Empty;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MsgBox.Error(ex.Message);
            }
        }
 
         

 

 

數據源

            DataTable dt = new DataTable();
            dt.Columns.Add("科目名稱", typeof(string));
            dt.Columns.Add("題目", typeof(bool));

            DataRow dr = dt.NewRow();
            dr["科目名稱"] = "000";
            dr["題目"] = true;
           
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;

 

//使用

dgv.CurrentRow  獲取選中的行
dgv.Rows[index].Selected
= true; 選中指定行 dgv.Rows[index].Cells["工號"].Selected = true; //選中指定行 public DataGridViewRow objs; 保存選中的行 objs.Cells["姓名"].Value.ToString() 獲得該行指定列的數據

 

 

 

 

dgv.CurrentRow  獲取選中的行

dgv.Rows[index].Selected = true;   選中指定行

   

dgv.Rows[index].Cells["工號"].Selected = true; //選中指定行

 

public DataGridViewRow objs;  保存選中的行

objs.Cells["姓名"].Value.ToString()  獲得該行指定列的數據

 

 

            //dgv.CurrentCell = dgv.Rows[10].Cells[1];//選中指定行
            //dgv.FirstDisplayedScrollingRowIndex = 10;//設置縱向滾動第一條數據

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM