Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,030

HOME > .NET Framework > Forum > อธิบายโค้ด1บรรทัด ให้อ่านหน่อยคะ DataGridViewCheckBoxCell = DirectCast(DataGridView1.CurrentCell, DataGridViewCheckBoxCell)



 

อธิบายโค้ด1บรรทัด ให้อ่านหน่อยคะ DataGridViewCheckBoxCell = DirectCast(DataGridView1.CurrentCell, DataGridViewCheckBoxCell)

 



Topic : 043263

Guest




Dim chk As DataGridViewCheckBoxCell = DirectCast(DataGridView1.CurrentCell, DataGridViewCheckBoxCell)

ไปก๊อปเขามาใช้อ่ะคะ แต่ไม่เข้าใจ แล้วมันก็errorบรรทัดนี้ด้วย คือว่าหนูมีDataGridViewอยู่ แล้วในนั้นก็มีCheckBox ในทุกๆแถวของข้อมูล เพื่อเอาไว้ติ๊กเลือกว่าจะอัพเดตแถวไหนบ้าง แล้วมันก็มีerrorอ่ะคะ (win app)vb.net

อันนี้เป็นโค้ดในส่วนการทำงานนั้นคะ ถ้ากดปุ่มมันก็จะอัพเดตแถวที่ได้ติ๊กเลือก

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim connString As New SqlConnection("packet size=4096;UID=**;password=**;data source='" & Label4.Text & "'; persist security info=True;initial catalog='" & ComboBox1.SelectedValue & "';")
connString.Open()
Dim chk As DataGridViewCheckBoxCell = DirectCast(DataGridView1.CurrentCell, DataGridViewCheckBoxCell)
chk.Value = 1
Dim update As String = "update bcapinvoicesub set groupcode='" & DataGridView1.CurrentRow.Cells("รหัสกรุ๊ป1").Value.ToString() & "' where itemcode='" & DataGridView1.CurrentRow.Cells("รหัสสินค้า").Value.ToString() & "'"
Dim Cmd = New SqlCommand(update, connString)
Cmd.ExecuteNonQuery()
Dim update1 As String = "update bcitem set groupcode='" & DataGridView1.CurrentRow.Cells("รหัสกรุ๊ป2").Value.ToString() & "' where code='" & DataGridView1.CurrentRow.Cells("รหัสสินค้า").Value.ToString() & "'"
Dim Cmd1 = New SqlCommand(update1, connString)
Cmd1.ExecuteNonQuery()
MsgBox("เรียบร้อย", MsgBoxStyle.OkOnly, "ผลการอัพเดต")
End Sub



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-05-25 09:32:15 By : ^,^ View : 4055 Reply : 20
 

 

No. 1



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


กดปุ่ม

อ้างอิง control checkbox ใน datagridview

ตรวงสอบ checkbox

ีupdate ฐานข้อมูล

Code (VB.NET)
Dim chk As DataGridViewCheckBoxCell = DirectCast(DataGridView1.CurrentCell, DataGridViewCheckBoxCell)


เปลี่ยน type ของ object DataGridView1.CurrentCell เป็น type DataGridViewCheckBoxCell






Date : 2010-05-25 11:50:24 By : tungman
 


 

No. 2

Guest


มันขึ้น error แบบนี้อ่ะคะ

Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewCheckBoxCell'.
Date : 2010-05-25 11:57:31 By : ^,^
 

 

No. 3



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


แสดงว่า CurrentCell มันไม่สามารถแปลงเป็น checkbox ได้ครับ
Date : 2010-05-25 12:02:38 By : tungman
 


 

No. 4

Guest


แล้วเปลี่ยน type ตรงไหนอ่ะคะ
Date : 2010-05-25 12:06:04 By : ^,^
 


 

No. 5

Guest


ทำไงต่ออ่ะคะ ถึงจะอัพเดตแถวที่เลือกหลายๆแถวได้
Date : 2010-05-25 12:07:09 By : ^,^
 


 

No. 6



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


จะเปลี่ยนมันต้องคำนึงถึง ต้นฉบับ มันด้วยครับ

ถ้า cell นั้นเป็น string คุณจะเอามาแปลงเป็น checkbox มันก็ error

control ที่อยู่ภายใน datagridview มันจะรู้จักเป็น object เวลาเอามาใช้งาน

เราต้องรู้เองว่า control นั้นคืออะไรเพื่อกำนหด type ให้มันได้ถูกต้อง
Date : 2010-05-25 12:09:32 By : tungman
 


 

No. 7

Guest


แล้วหนูจะไปต่อยังไงอ่ะคะ ไม่งั้นให้userอัพเดตที่ละแถว เขาบ่นหูแย่เลย พอมีทางออกไหมคะ
Date : 2010-05-25 12:59:42 By : ^,^
 


 

No. 8



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


อ้าว ไม่แก้ตรงนี้ก่อนเหรอ
Date : 2010-05-25 13:03:30 By : tungman
 


 

No. 9

Guest


เริ่มแก้ยังไงอะคะ หนูเริ่มไม่ถูก
Date : 2010-05-25 13:17:44 By : ^,^
 


 

No. 10



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ต้องเริ่มดูที่ datagridview ว่าหน้าตาเป็นแบบไหน มี control อะไร ใช้ทำอะไรบ้างก่อน

เขียนมาแค่นี้นึกไม่ออกหรอก
Date : 2010-05-25 13:24:18 By : tungman
 


 

No. 11

Guest


datagridview มี 4 คอลัม ประกอบด้วย 3คอลัม ที่เป็นการดึงข้อมูลจากดาต้าเบสมาแสดงตามเงื่อนไข ส่วนอีก1คอลัม เป็นcheckbox ที่เราแอดเข้าไปใน datagridview เพื่อไว้เลือกว่าจะอัพเดตไหม โดยที่ datagridviewนั้น สามารถ edit กับ delete ได้

ซึ่งตอนนี้มันอัพเดตเฉพาะแถวปัจจุบันเท่านั้น ถ้าเขียนลูปให้มันวนเช็คทุกแถวข้อมูล ว่าแถวไหนมีการเลือก checkbox บ้าง แล้วให้มันทำการอัพเดตแถวพวกนั้นทั้งหมด จะได้ไหมคะ แล้วมันเขียนประมาณไหนอ่ะคะ
Date : 2010-05-25 13:39:34 By : ^,^
 


 

No. 12



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


หน้าตาประมาณนี้หรือเปล่า

1
Date : 2010-05-25 13:55:14 By : tungman
 


 

No. 13

Guest


ก็ประมาณนั้นคะ แต่ไม่ลบ อัพเดตอย่างเดียว
Date : 2010-05-25 14:02:47 By : ^,^
 


 

No. 14



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ติดไว้ก่อน ค่ำๆ จะดูให้ เพราะถ้าทำต้องมีคงต้องใช้ notepad เขียน แล้ว compile เอง
Date : 2010-05-25 14:18:34 By : tungman
 


 

No. 15

Guest


รอก็ได้คะ ทำไม่เป็นเอง ขอบคุณมากนะคะ
Date : 2010-05-25 14:20:09 By : ^,^
 


 

No. 16



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


จริงๆ ลองเองก็ได้นะ หัดเปิดแล้วทำตาม msdn อยู่
Date : 2010-05-25 14:26:34 By : tungman
 


 

No. 17

Guest


หนูก็พยายามอยู่เหมือนกันคะ แต่ไม่รู้ว่าจะตรวจสอบ CheckBox ยังไง
Date : 2010-05-25 15:00:17 By : ^,^
 


 

No. 18



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


สร้างโปรเจ็คใหม่ ตั้งชื่อว่า MyDataGridView แล้วก็อปโค้ดนี้ไปวางไว้ใน Form1.cs

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyDataGridView
{
    public partial class Form1 : Form
    {
        private System.Data.DataTable DtSource;
        private System.Data.DataTable DtTemp;

        private System.Windows.Forms.DataGridView dataGridView1;

        private System.Windows.Forms.Button editButton;
        private System.Windows.Forms.Button saveButton;
        private System.Windows.Forms.Button cancelButton;

        public Form1()
        {
            InitializeComponent();

            this.DtSource = new System.Data.DataTable();
            this.DtTemp = new System.Data.DataTable();

            this.dataGridView1 = new System.Windows.Forms.DataGridView();

            this.editButton = new System.Windows.Forms.Button();
            this.cancelButton = new System.Windows.Forms.Button();
            this.saveButton = new System.Windows.Forms.Button();

            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(12, 12);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(268, 208);
            this.dataGridView1.TabIndex = 0;
            // 
            // editButton
            // 
            this.editButton.Location = new System.Drawing.Point(205, 231);
            this.editButton.Name = "editButton";
            this.editButton.Size = new System.Drawing.Size(75, 23);
            this.editButton.TabIndex = 1;
            this.editButton.Text = "Edit";
            this.editButton.UseVisualStyleBackColor = true;
            this.editButton.Click += new System.EventHandler(this.editButton_Click);
            // 
            // SaveButton
            // 
            this.saveButton.Location = new System.Drawing.Point(123, 231);
            this.saveButton.Name = "SaveButton";
            this.saveButton.Size = new System.Drawing.Size(75, 23);
            this.saveButton.TabIndex = 1;
            this.saveButton.Text = "Save";
            this.saveButton.UseVisualStyleBackColor = true;
            this.saveButton.Hide();
            this.saveButton.Click += new EventHandler(saveButton_Click);
            // 
            // CancelButton
            // 
            this.cancelButton.Location = new System.Drawing.Point(204, 231);
            this.cancelButton.Name = "CancelButton";
            this.cancelButton.Size = new System.Drawing.Size(75, 23);
            this.cancelButton.TabIndex = 2;
            this.cancelButton.Text = "Cancel";
            this.cancelButton.UseVisualStyleBackColor = true;
            this.cancelButton.Hide();
            this.cancelButton.Click += new EventHandler(cancelButton_Click);

            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.saveButton);
            this.Controls.Add(this.cancelButton);
            this.Controls.Add(this.editButton);

            DataGridViewCheckBoxColumn CheckBoxColumn = new DataGridViewCheckBoxColumn();
            CheckBoxColumn.ReadOnly = false;
            CheckBoxColumn.DisplayIndex = 0;
            CheckBoxColumn.DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
            CheckBoxColumn.TrueValue = true;
            CheckBoxColumn.FalseValue = false;
            CheckBoxColumn.Width = 20;

            DataGridViewTextBoxColumn IdColumn = new DataGridViewTextBoxColumn();
            IdColumn.ReadOnly = true;
            IdColumn.HeaderText = "ID";
            IdColumn.DefaultCellStyle.BackColor = System.Drawing.Color.LightGray; ;
            IdColumn.DataPropertyName = "ID";

            DataGridViewTextBoxColumn NameColumn = new DataGridViewTextBoxColumn();
            NameColumn.ReadOnly = true;
            NameColumn.HeaderText = "Name";
            NameColumn.DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
            NameColumn.DataPropertyName = "DayName"; 
            
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;

            dataGridView1.Columns.Add(CheckBoxColumn);
            dataGridView1.Columns.Add(IdColumn);
            dataGridView1.Columns.Add(NameColumn);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DtSource = GetInitialData();
            dataGridView1.DataSource = DtSource.Copy();
        }

        private DataTable GetInitialData()
        {
            string[] DayArray = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
            int Count = 0;

            DataTable Dt = new DataTable("DayTable");
            Dt.Columns.Add(new DataColumn("ID", System.Type.GetType("System.Int16")));
            Dt.Columns.Add(new DataColumn("DayName", System.Type.GetType("System.String")));

            foreach (string aDay in DayArray)
            {
                DataRow Dr = Dt.NewRow();
                Dr["ID"] = ++Count;
                Dr["DayName"] = aDay;
                Dt.Rows.Add(Dr);
            }

            return Dt;
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            int isCheck = 0;

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value) == true)
                {
                    row.Cells[0].Style.BackColor = System.Drawing.Color.White;
                    row.Cells[1].Style.BackColor = System.Drawing.Color.White;
                    row.Cells[2].Style.BackColor = System.Drawing.Color.White;

                    row.Cells[1].ReadOnly = false;
                    row.Cells[2].ReadOnly = false;
                    isCheck++;
                }

                row.Cells[0].ReadOnly = true;
            }

            if (isCheck > 0)
            {
                DtTemp = (dataGridView1.DataSource as DataTable).Copy();

                editButton.Hide();
                saveButton.Show();
                cancelButton.Show();
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        { 
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value) == true)
                {
                    row.Cells[0].Style.BackColor = System.Drawing.Color.LightGray;
                    row.Cells[1].Style.BackColor = System.Drawing.Color.LightGray;
                    row.Cells[2].Style.BackColor = System.Drawing.Color.LightGray;
                }

                row.Cells[0].Value = false;
                while (row.Cells[1].ReadOnly == false) row.Cells[1].ReadOnly = true;
                while (row.Cells[2].ReadOnly == false) row.Cells[2].ReadOnly = true;
            }

            DtSource = (dataGridView1.DataSource as DataTable).Copy();

            editButton.Show();
            saveButton.Hide();
            cancelButton.Hide();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value) == true)
                {
                    row.Cells[0].Style.BackColor = System.Drawing.Color.LightGray;
                    row.Cells[1].Style.BackColor = System.Drawing.Color.LightGray;
                    row.Cells[2].Style.BackColor = System.Drawing.Color.LightGray;
                }

                row.Cells[0].Value = false;
                while (row.Cells[1].ReadOnly == false) row.Cells[1].ReadOnly = true;
                while (row.Cells[2].ReadOnly == false) row.Cells[2].ReadOnly = true;
            }

            dataGridView1.DataSource = DtTemp.Copy();

            editButton.Show();
            saveButton.Hide();
            cancelButton.Hide();
        }
    }
}

Date : 2010-05-25 21:35:02 By : tungman
 


 

No. 19

Guest


มันเหมือนกับ vb หรอคะ หนูเขียน vb อ่ะ คือ ที่หนูเขียนไว้มันประมาณนี้อ่ะคะ แต่ว่ามันยัง error อยู่ ตรงบรรทัด dim chk อ่ะคะ ไม่รู้จะแปลงค่ายังไง หนูดูโค้ดที่พี่อุตส่าห์เขียนให้ดู ก็ยังงงๆอยู่อ่ะคะ

Code (VB.NET)
 For i As Integer = 1 To DataGridView1.Rows.Count - 1
            Dim chk As DataGridViewCheckBoxCell = boolean(DataGridView1.Rows(i), DataGridViewCheckBoxCell)
            If chk.Value = True Then
                Dim update As String = "update bcapinvoicesub set groupcode='" & DataGridView1.CurrentRow.Cells("รหัสกรุ๊ป1").Value.ToString() & "' where itemcode='" & DataGridView1.CurrentRow.Cells("รหัสสินค้า").Value.ToString() & "'"
                Dim Cmd = New SqlCommand(update, connString)
                Cmd.ExecuteNonQuery()
                Dim update1 As String = "update bcitem set groupcode='" & DataGridView1.CurrentRow.Cells("รหัสกรุ๊ป2").Value.ToString() & "' where code='" & DataGridView1.CurrentRow.Cells("รหัสสินค้า").Value.ToString() & "'"
                Dim Cmd1 = New SqlCommand(update1, connString)
                Cmd1.ExecuteNonQuery()
                MsgBox("เรียบร้อย", MsgBoxStyle.OkOnly, "ผลการอัพเดต")
            End If
   Next i

Date : 2010-05-26 09:42:12 By : ^,^
 


 

No. 20

Guest


หนูทำจนได้แล้วแหละคะ
ขอบคุณพี่ tungman มากนะคะ
Date : 2010-05-26 16:56:38 By : ^,^
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : อธิบายโค้ด1บรรทัด ให้อ่านหน่อยคะ DataGridViewCheckBoxCell = DirectCast(DataGridView1.CurrentCell, DataGridViewCheckBoxCell)
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่