을 얻을 datagrid 값에 의해 행에 색인을 위해 나중에 사용하기

0

질문

내가 노력하고의 값을 얻을 수의 세포의 datagrid 행에 저장하는 나중에 사용할 수 있지만,그것은 보이는 내가 그것을 얻을 수 없습니다.

클릭 행을 만들어야 메뉴에 표시되는가를 수행하도록 선택할 수 있습니다 행동으로 해당 값이 적용됩니다.

이것은 무엇을 나는 지금까지 달성

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs x)
        {
            if (dataGridView1.Rows[x.RowIndex].Cells["Name"].Value != null) name = dataGridView1.Rows[x.RowIndex].Cells["Name"].Value.ToString();
            else if (dataGridView1.Rows[x.RowIndex].Cells["LastName"].Value != null) last = dataGridView1.Rows[x.RowIndex].Cells["LastName"].Value.ToString();
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (last != null && name != null)
                {
                    ContextMenu cm = new ContextMenu();
                    this.ContextMenu = cm;
                    cm.MenuItems.Add(new MenuItem("&Do something with those values in this row", new System.EventHandler(this.do_Action_with_values)));
                    cm.Show(this, new Point(e.X, e.Y));
                }
                last = null;
                name = null;
            }
        }

을 제거하면(마지막!= null&&!이름= null)

이 메뉴를 만들지만,일 값이 저장되지 않습니다,그들은 null 입니다.

가 제대로 저장하는 방법에는 문자열을 모두 열 값에 대한 행 클릭하니까?

c# datagrid
2021-11-23 17:36:12
1

최고의 응답

0

당 요청서 코멘트 사용 CellMouseClick 이벤트 DataGridViewCellMouseEventArgs 결합하는 당신의 처리기로 하나 이벤트를 처리하는 모든 속성을 필요합니다.

Buyer,아래의 코드를 작성되었다 외부의 IDE 이 있을 수 있습니다 그래서 구문/기타 오류가 있습니다.

    private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button != MouseButtons.Right) return;
        int row = e.RowIndex;
        string name = dataGridView1.Rows[row].Cells["Name"].Value;
        string last = dataGridView1.Rows[row].Cells["LastName"].Value;
        if (name == null || name.Trim().Length == 0) return;
        if (last == null || last.Trim().Length == 0) return;
        ContextMenu cm = new ContextMenu();
        this.ContextMenu = cm;
        cm.MenuItems.Add(new MenuItem("&Do something with those values in this row", new System.EventHandler(this.do_Action_with_values)));
        cm.Show(this, new Point(e.X, e.Y));
    }
2021-11-23 18:39:27

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................