瀏覽次數: 627316

  • 如何設定DataGridView的header的text顏色?
    如果DataGridView的ColumnType為InfoDataGridViewTextBox類型時,可以設定其HeadCellStyle屬性,但是,Header標題的顏色只有在windows經典風格下才會有效,如果不是經典的模式,建議改用粗體或者加*來提示。



  • 如何在GridView中實現,當條件為=A欄位的值,取B欄位的值加總?
    此例為,如果Type="001"則加總QTY.請依照自己情況作相應修改.建議用InfoBindingsource處理比較好.可在Navigator的BeforeItemClick事件中進行。
    int i = ibsDetail.List.Count;
    int qty = 0;
    string sType = "";
    for (int j = 0; j < i; j++)
    {
    sType = ((DataRowView)ibsDetail.List[j])["TYPE"].ToString();
    if (sType == "001")
    qty = qty + Convert.ToInt16(((DataRowView)ibsDetail.List[j])["QTY"]);
    }




  • infoDataGridView 的TotalChanged如果有多個Total 要處理,如何知道是第幾個的值?
    TotalChanged的事件中,DataGridViewTotalChangedEventArgs e,利用e.ColumnName可以作為你判斷當前total的是哪個欄位,可以加以處理。



  • 如何設計將InfoDataGridView的Detail 某個欄位Total,回寫到Master的某個元件上?
    有兩種方法:
    1. 使用GridView上的計算Total的功能ExpressionColumn,配合GridView的Total方式可以對該欄位元進行加總。同時為了配合Total方式的加總,GridView提供了TotalChanged事件,由此可以取得加總值。
    if (e.ColumnName == "Amount" && (infoNavigator1.GetCurrentState() == "Inserting" || infoNavigator1.GetCurrentState() == "Editing"))
    {
    (ibsMaster.Current as DataRowView).Row.BeginEdit();
    (ibsMaster.Current as DataRowView).Row["TotalAmt"] = Convert.ToDecimal(e.TotalValue);
    (ibsMaster.Current as DataRowView).Row.EndEdit();
    //此方法的作用是為了當Master沒有修改的狀態下可以將這行Row的狀態變為Modifed
    }

    2. 可以在InfoDataGridView的CellValueChanged中寫程式,同時加一下條件判斷:
    if (infonavigator.GetCurrentState() == "Inserting" || infonavigator.GetCurrentState() == "Editing" )
    {
    if (e.ColumnIndex == yourIndex)
    textbox1.Text = infoDataGridVeiw.DoSum(colIndex, totalMode).ToString();//colIndex為列的索引,totalMode為匯總的類型
    }




  • 如何在infodatagridview中,將有運算式欄位來排序?
    參考方法如:
    先將該運算式欄位的SortMode設為Programmatic,在InfoDataGridView的CellClick事件中設計,假設運算式欄位為EMPTextBoxColumn:
    private void infoDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
    if (e.RowIndex == -1 && e.ColumnIndex == this.infoDataGridView1.Columns.IndexOf(EMPTextBoxColumn))
    {
    if (infoDataGridView1.SortedColumn == EMPTextBoxColumn && infoDataGridView1.SortOrder == SortOrder.Ascending)
    {
    this.ibsMaster.Sort = "EMP desc"; // ibsMaster為對應的InfoBindingSource
    }
    else
    {
    this.ibsMaster.Sort = "EMP asc";
    }
    }
    }




  • GridView產生的Total欄位怎麼讓它靠右對齊呢?
    在Columns中設定該欄位的ItemStyle.HorizontalAlign設定微Right。



  • DataGridView中如何取出被選取資料內容值;如何撰寫全部選取?
    1.infoDataGridView.SelectedRows就是當前選中的所有的Row。
    用infoDataGridView. SelectedRows[index].Cells[index].Value來取目前資料的內容值。
    2. infoDataGridView.SelectAll();這個是全部選中。
  • 如何設定DataGridView的header的text顏色?
    如果DataGridView的ColumnType為InfoDataGridViewTextBox類型時,可以設定其HeadCellStyle屬性,但是,Header標題的顏色只有在windows經典風格下才會有效,如果不是經典的模式,建議改用粗體或者加*來提示。
  • Detail不是主鍵的欄位,如何做到可以新增卻不能更改?
    可以在Detail的GridView設定CellBeginEdit事件,如下:
    private void infoDataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
    if (e.RowIndex != infoDataGridView1.NewRowIndex) //更改而不是新增
    {
    if (e.ColumnIndex == 不希望修改的欄位在的列號)
    {
    e.Cancel = true;//拒絕修改
    }
    }
    }
  • 為何在DataGridView中要刪除一筆資料,按 Delete鍵時,卻只是刪除第一個 Column的字元?
    如果設定EditMode為EditOnEnter,那麼在選中的時候,常常會自動進入某個Cell的編輯狀態,這時候,鍵盤的操作對象是當前的Cell,而非GridView,所以這時候是無法刪除整筆記錄的。可以讓User點選到DataGridView的Header然後再按Delete鍵即可刪除。
  • infoDataGridView 的Total值, 有時為何顯示就不完整?
    這部分需要調整一下DataGridView的Rows高度,在設計模式中,請調整DataGridView讓最後一Row能完全看到(或是多出幾個點), 即可解決此問題。
  • 如何在某一個DataGridViewTextBoxColumn,輸入40,轉變為0040?
    寫在DataGridView的CellValueChanged事件中,如下:
    if (infoNavigator1.CurrentState == "Editing" ||infoNavigator1.CurrentState == "Inserting")
    {
    if (e.ColumnIndex== 需要修改的欄位的列數)
    {
    if (InfoDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString().Length < 4)
    InfoDataGridView[e.ColumnIndex, e.RowIndex].Value = "00" + InfoDataGridView [e.ColumnIndex, e.RowIndex].Value.ToString();
    }
    }
    前面的判斷是為了只在編輯或者新增資料的時候才這樣處理,其他如瀏覽等狀態不必處理。
  • 用程式新增Detail資料,為何新增資料不會立刻顯示出來?
    可在Detail新增程式的最後加一句:GridView.Refresh();
    或者你可以改成直接對GridView的cell設定值,這樣就可以即時顯示,又不用額外處理。
  • 當InfoDataGridView中使用infoRefVal編輯時,怎麼透過對InfoDataGridView的取得infoRefVal的DisplayMember的值,而不是實際的ValueMember的值?
    grdDetail[ColumnIndex, RowIndex].FormattedValue是取得DisplayMember的內容;
    grdDetail[ColumnIndex, RowIndex].Value是取得ValueMember的內容。
  • 如何在打開Form1的時候定位infoDataGridView的某一筆指定資料?
    可以在Form_Load的事件中,以下列程式來處理:
    DataRow[] drs = infoDataSet1.RealDataSet.Tables[0].Select("CustomerID=yourID"); // 取得DataRow
    int index = infoDataSet1.RealDataSet.Tables[0].Rows.IndexOf(drs[0]);//取得符合條件的第一筆
    infoBindingSource1.Position = index;
  • 有一個infoDataGridView,想要把重複的資料整列用特殊顏色標記出來?
    private void infoDataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
    if (e.ColumnIndex == 0)//當資料欄位Index
    {
    if (infoDataGridView2[0, e.RowIndex].Value != null)//當欲比對之資料不為空時才判斷
    {
    DataSet ds = idTemp.Execute("select autono, …from …..”);
    int i =ds.Tables[0].Rows.Count;//算出總筆數
    int j = 0;
    while (j <= i)
    {
    string NameRedColor = ds.Tables[0].Rows[j]["autono"].ToString();//取得相同資料
    if (infoDataGridView2[23, e.RowIndex].Value.ToString() == NameRedColor)//如果資料相同則改變當前欄位處的顏色
    {
    Gridview.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Pink;
    }
    j = j + 1;
    }
    }
    }
    }
  • 為何在InfoDataGridView中,把Key Field設定為Visible=false,離開後又恢復為true?
    這是VS本身的限制,請不要把InfoDataGridView的KeyField這個欄位放在第一個,而且又設為Visible=False。
  • InfoDataGridView 的RefValColumn是否可以用熱鍵打開查詢窗體?
    GridviewRefvalColumn有一個內定的熱鍵(快捷鍵)來打開窗體,即使用(ctrl+enter)即可,這樣也可以不用每個refvalColumn都寫程式控制了。
  • 如何透過下SQL語法將資料內容動態顯示在DataGridView中?
    只要設定DataGridView的DataSource設定為DataSet,DataMember設定為DataTable的名字就可以了.如:
    DataSet ds = CliUtils.ExcuteSql(“Select ..你的sql語句..”);
    DataGridView1.DataSource = ds;
    DataGridView1.DataMember = ds.Tables[0].TableName;
  • 如何控制在infoGridView中的一個Cell做更改後,按向下鍵(Down),則讓遊標移到下一個Row的這個欄位,不要跑到第一個column去?(AutoApply設為True)
    可以在InfoDataSet的BeforeApplyUpdates事件中記下位置,然後在AfterApplyUpdates事件中還原,如下:

    Point cellPoint = Point.Empty; // 設定一個Local變數
    private void infoDataSet1_BeforeApplyUpdates(object sender, EventArgs e)
    {
    if (this.infoDataGridView1.SelectedCells.Count == 1)
    {
    DataGridViewCell cell = this.infoDataGridView1.SelectedCells[0];
    cellPoint = new Point(cell.ColumnIndex, cell.RowIndex); // 紀錄
    }
    }

    private void infoDataSet1_AfterApplyUpdates(object sender, EventArgs e)
    {
    if (cellPoint != Point.Empty && this.infoDataGridView1.SelectedCells.Count == 1)
    {
    this.infoDataGridView1.SelectedCells[0].Selected = false;
    this.infoDataGridView1[cellPoint.X, cellPoint.Y].Selected = true; // 還原
    cellPoint = Point.Empty;
    }
    }