DataGridViewでの右クリックメニューの作り方
画面を作り込んでいく中でDataGridViewの情報を削除するUIに悩んだ結果
コンテキストメニューに「削除」を表示する方法を試してみた。
メニューがでる位置が一定ではないのが気になるが・・・・
とりあえず↓のようなロジックで行った。
private void dgv_Keiyaku_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
// 選択行の連番号を取得して削除のキーに使う
int dgv_Keiyaku_SelectedRowNo = Int32.Parse(dgv_Keiyaku.CurrentRow.Index.ToString());
dgv_Keiyaku_SelectedSeqNo = Int32.Parse(dgv_Keiyaku[8, dgv_Keiyaku_SelectedRowNo].Value.ToString());
if (e.Button == MouseButtons.Right)
{
// マウスがクリックされた座標(e.X, e.Y)から、
ContextMenuStrip contexMenu = new ContextMenuStrip();
contexMenu.Items.Add("削除");
contexMenu.ItemClicked += new ToolStripItemClickedEventHandler(contexMenu_ItemClicked);
contexMenu.Show(dgv_Keiyaku, new Point(e.X, e.Y));
}
if (e.RowIndex != -1 && e.ColumnIndex != -1)
{
if (e.Button == MouseButtons.Right)
{
DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];
// Here you can do whatever you want with the cell
this.dgv_Keiyaku.CurrentCell = clickedCell; // Select the clicked cell, for instance
// Get mouse position relative to the vehicles grid
var relativeMousePosition = dgv_Keiyaku.PointToClient(Cursor.Position);
// Show the context menu
ContextMenuStrip contexMenu = new ContextMenuStrip();
contexMenu.Show(dgv_Keiyaku, relativeMousePosition);
}
}
//if (e.Button == MouseButtons.Right)
//{
// ContextMenu m = new ContextMenu();
// m.MenuItems.Add(new MenuItem("Cut"));
// m.MenuItems.Add(new MenuItem("Copy"));
// m.MenuItems.Add(new MenuItem("Paste"));
// int currentMouseOverRow = dgv_Keiyaku.HitTest(e.X, e.Y).RowIndex;
// if (currentMouseOverRow >= 0)
// {
// m.MenuItems.Add(new MenuItem(string.Format("Do something to row {0}", currentMouseOverRow.ToString())));
// }
// Console.WriteLine("X({0}, Y({1})", e.X, e.Y);
// m.Show(dgv_Keiyaku, new Point(e.X, e.Y));
//}
}
object をNULL判定する
object をNULL判定するのに困った。
ただのObjectではなく
NpgsqlDataReader を使って
PostgreからDataをGetした時に
当該カラムがnullなのか否かを判定するが
全くnullと判定できなかった。
「?」を使ってやると .ToString()がエラーにならなくなった。
ただ左辺に代入した変数は "" のようだ。
//SQLの実行
npg_reader = Class_Npg.Exe_Sql(str_sql);
while (npg_reader.Read())
{
// NULLなら空文字となるのか?
var obj = npg_reader.GetValue(9)?.ToString();
if ( (string)obj == "")
{
this.Dgv[9, int_count].Value = " ";
}
else
{
this.Dgv[9, int_count].Value = npg_reader.GetValue(9);
}
}
C# DateTimePickerで和暦表示をする方法で困った
C# DateTimePickerで和暦表示をする方法で困ったのでメモる。
コントロールに過去年の日付を渡しているのだが
表示される年は今年になってしまう?????
コントロールのValueChanged EventHandlerに記述が必要というのがわかった。
private void dtp_ValueChanged(object sender, EventArgs e)
{
System.Globalization.Calendar calendar = new System.Globalization.JapaneseCalendar();
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("ja-JP");
culture.DateTimeFormat.Calendar = calendar;
DateTimePicker dtp = (DateTimePicker)sender;
dtp.CustomFormat
= dtp.Value.ToString("gg yy", culture) + "年MM月dd日";
}
数字を3桁区切りで画面表示
C#で数字を3桁区切りで画面表示したい場合に使える。
// 単価を3桁カンマ表示
lbl.Text = string.Format("{0:#,0}", Int32.Parse(_Tanka.ToString()));
【C#】 コンボボックスへ選択状態を表示する
コンボボックスでDBに登録されている文字列を
そのまま選択されている状態で表示する場合の手法です。
[コンボボックス名称].SelectedIndex
= [コンボボックス名称].FindStringExact(”選択文字列”);
【C#】 画面コントロールの種別を見て処理を行う
C# で 画面コントロールの種別を見て処理を行う事があります。
ちょっと強引ですが、こんな感じでやりました。
他にもっといい手法があるはずなので
また更新します。
private void Data_Enabled(Boolean bbool)
{
foreach (Control c in MainPanel.Controls)
{
if (c.GetType().Equals(typeof(TextBox)))
{
if (kindNowValue == SyoriKind.UserCareLvl &&
c.Name == "txt_1")
{
c.Enabled = !bbool;
}
else if (c.Name != "txt_0")
{
c.Enabled = bbool;
}
}
else if (c.GetType().Equals(typeof(ComboBox)))
{
c.Enabled = bbool;
if(!bbool)
{
ComboBox tmp = (ComboBox)c;
tmp.SelectedIndex = -1;
}
}
else if (c.GetType().Equals(typeof(DateTimePicker)))
{
c.Enabled = bbool;
}
else if (c.GetType().Equals(typeof(Button)))
{
c.Enabled = bbool;
}
}
}
【C#】区切り文字で分割
業務アプリを作っていてよく出てくるのが
指定した区切り文字で文字列を分割する方法です。
C#で書く場合の
Splitメソッドを使う方法を書いておく。
private void stringSplit(string argStr)
{
string[] textArray;
textArray = argStr.Split(','); // 文字列を分割
listBox.Items.Clear();
foreach(string txt in textArray)
{
listBox.Items.Add(txt); // リストボックスに入れる
}
}