 |
|
{C#} การ Clear ค่าใน Combobox ทำอย่างไรครับ ตอนนี้ข้อมูลใน Combobox มันเพิ่มมาเป็น 1 รอบหลังการทำงานจบหนึ่งคำสั่งครับ |
|
 |
|
|
 |
 |
|
cmbUnitCode.Items.Clear();
ก่อนเริ่มบรรทัดที่ 1 ตามตัวอย่าง ลองดูนะครับ ผมก็มือใหม่ แต่น่าจะได้แหละ
cmbUnitCode <== ชื่อ Combobox ที่ต้องการ Clear นะ
|
 |
 |
 |
 |
Date :
2011-11-16 07:51:41 |
By :
memmo12 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
cmbUnitCode.Items.Clear();
มันErrOr ว่า
Items collection cannot be modified when the DataSource property is set.
|
 |
 |
 |
 |
Date :
2011-11-16 09:31:14 |
By :
pissanu1988 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองใส่ ds.Tables[ "Unt" ].Clear();
ก่อน Unt.Fill(ds, "Unt");
ดูครับเผื่อได้ 
|
 |
 |
 |
 |
Date :
2011-11-16 10:41:41 |
By :
Programmer Of Persia |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ไม่ได้ครับ
|
 |
 |
 |
 |
Date :
2011-11-16 10:57:23 |
By :
pissanu1988 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2011-11-16 11:00:52 |
By :
pissanu1988 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผม ว่า น่าจะลองเขียน เงื่ยนไขนะครับ ว่าถ้าถูกเรียมแล้ว ก็ไม่ต้องเรียกอีก ประมาณนี้ปะครับ แล้วจะเขียนยังงัยดีครับ
|
 |
 |
 |
 |
Date :
2011-11-16 11:04:35 |
By :
pissanu1988 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
CBB.item.clear(); <---- เป็นการเคลียร์ค่าที่ Add ใส่ใน CBB
Code (C#)
if (DS.Tables.Contains("___"))
{
DS.Tables["___"].Clear(); <----- เป็นการเคลียร์ค่าที่ Source เข้าไปใน CBB
}
เวลาใช้งาน ! ต้องให้มันทำงานเสร็จก่อนแล้วค่อยเคลียร์ค่าครับ จากนั้นก็ Source หรือ Add เข้าไปใหม่ครับ !
Code (C#)
SqlDataAdapter Unt = new SqlDataAdapter("SELECT * FROM Unit ", Conn);
Unt.Fill(ds, "Unt");
cmbUnitCode.DataSource = ds;
cmbUnitCode.DisplayMember = "Unt.UnitCode";
cmbUnitCode.ValueMember = "Unt.UnitCode";
cmbUnitName.DataSource = ds;
cmbUnitName.DisplayMember = "Unt.UnitName";
cmbUnitName.ValueMember = "Unt.UnitCode";
จาก Code ด้านบนของคุณ ผมไม่แน่ใจว่าเลือก CBB อันหนึ่ง แล้วอีกอันมันจะไม่เลือกตามเหรอครับ ผมทำแล้วมันเลือกตาม หรือว่าผมทำมั่วก็ไม่รู้ 555+
|
 |
 |
 |
 |
Date :
2011-11-16 11:13:10 |
By :
DevAMZ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ
Code (VB.NET)
Dim sqlPrename As String
sqlPrename = "SELECT * FROM Prename ORDER BY PrenameCode"
Dim dtPrename As DataTable
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
With com
.CommandType = CommandType.Text
.CommandText = sqlPrename
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
dtPrename = New DataTable()
dtPrename.Load(dr)
bsPrename = New BindingSource()
bsPrename.DataSource = dtPrename
With cboPrename
.DataSource = dtPrename
.DisplayMember = "PrenameName"
.ValueMember = "PrenameCode"
bdPrename = .DataBindings.Add("SelectedValue", bsPrename, "PrenameCode", True)
End With
Else
MessageBox.Show("ไม่มีข้อมูลคำนำหน้าชื่อ...กรุณาเพิ่มข้อมูลคำนำหน้าชื่อก่อน!!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End With
ช่วย แปลง Code Vb เป็น C# ให้หน่อยครับ
|
 |
 |
 |
 |
Date :
2011-11-16 11:42:13 |
By :
pissanu1988 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|