เอา 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;
            }
        }

ลบ:หาก(สุดท้าย!= โพรโทคอล aimcomment&&ชื่อของ!= โพรโทคอล aimcomment)

จะทำให้เมนูทำงานแต่งค่าของไม่ได้ช่วยไว้ทั้งหมดเป็นโพรโทคอล aimcomment.

มีเหมาะสมทางร้านอยู่ในใช้เส้นสายทั้งหมดคอลัมน์คุณค่าสำหรับคนแถวที่ถูกคลิก?

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

คำตอบที่ดีที่สุด

0

ต่อการร้องขออยู่ในความคิดเห็นใช้ CellMouseClick เหตุการณ์แล้ว DataGridViewCellMouseEventArgs เพื่อรวมของคุณมอแฮนเดลร์สองเหตุการณ์เครื่องมือจัดการมันทั้งหมดคุณสมบัติของคุณต้องการ

คนซื้อจงระวัง,รหัสด้านล่างนี้ถูกเขียนขึ้นด้านนอกของเป็นแบบ 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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่