1

バインディングソースを介してdatagridviewをdatatableにバインドするコードがあります。

_bind.DataSource = Table;
lGrid.DataSource = _bind;

DataBindingCompleteイベントで、いくつかの列にDefaultCellStyleを設定しました。

void lGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    if (e.ListChangedType == ListChangedType.Reset)
        lGrid.Columns[0].DefaultCellStyle.Format = "+#,##0;-#,##0;0";
}

この時点では、テーブルはまだ空です。したがって、後で行がテーブルに追加され、DataGridViewに適切に反映されると、何らかの理由で、フォーマットが列0のセルに適用されません。

CellFormattingイベント内のフォーマットを確認しました。

private void lGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs args)
{
    if (args.ColumnIndex == lGrid.Columns[0].Index)
    {
        string s1 = lGrid.Columns[0].DefaultCellStyle.Format;
        string s2 = lGrid[0, args.RowIndex].Style.Format;
    }
}

s1は正しい形式を返しますが、s2は単なる空の文字列です。

私が間違っていることと、セルをフォーマットする方法を教えてください。ありがとう!

4

2 に答える 2

4

CellFormattingイベントでセル スタイルをフォーマットしてみてください。

private void lGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs args) 
{
    if (args.ColumnIndex == 0)
    {
        args.Value = // format Value here
        args.FormattingApplied = true;
    }
}
于 2011-04-14T05:48:39.760 に答える
2

これは、DataTable 列のデータ型が文字列 (デフォルト) に設定されていることが原因である可能性があります。

したがって、DataTable 列のデータ型を数値に変更できます。

  1. 列を含む DataTable を作成する場合

    Table.Columns.Add(ColumnName,typeof(double));
    
  2. 日付の種類を後で変更する

    Table.Columns[0].DataType = typeof(decimal);
    
于 2014-12-03T08:16:29.197 に答える