PDA

View Full Version : سوال: اشکال این کد کجاست؟ تشخیص انتخاب چک باکس دیتاگرید



csharpdoost
جمعه 26 آذر 1400, 19:10 عصر
دوستان سلام. میخوام در ستون چک باکس دیتاگرید هر چک باکسی که تیک خورد یا تیکش برداشته شد همون موقع ثبت بشه. اما فقط قسمت دوم یعنی (else if) اجرا میشه.
نمیدونم اشکال کدم کجاست؟


private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{


if (e.RowIndex >= 0 && e.ColumnIndex == 0)
{

if (dataGridView1.Rows[e.RowIndex].Cells["checkBoxColumn"].Value != null && (bool)dataGridView1.Rows[e.RowIndex].Cells["checkBoxColumn"].Value)
{
listRowId.Add(e.RowIndex);
MessageBox.Show(e.RowIndex.ToString() + "checked");
}
else if (dataGridView1.Rows[e.RowIndex].Cells["checkBoxColumn"].Value == null)
{
listRowId.Remove(e.RowIndex);
MessageBox.Show(e.RowIndex.ToString() + "unchecked");
}

علیرضا حسن زاده
شنبه 27 آذر 1400, 17:52 عصر
سلام
تو رویداد CellContentClick بنویس کدت رو حل میشه

csharpdoost
شنبه 27 آذر 1400, 21:35 عصر
نه متاسفانه هیچ فرقی نکرد. توی هر رویدادی از رویدادهای دیتاگرید مینویسم فرقی نمیکنه و به همون صورت نشون میده. اما اگه توی رویداد باتن مینویسم و بعد از انتخاب چک باکسها دکمه باتن رو اجرا میکنه درست نشون میده. (کد زیر). اما مشکل من اولویت بندی انتخاب چک باکسها هستش. یعنی هر چک باکسی که اول انتخاب شد در اولویت اول ثبت بشه و همینطور به ترتیب ... .


private void btnRun_Click(object sender, EventArgs e)
{



int rr = 0;
List<int> listRowId = new List<int>();


foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell;


bool isSelected = Convert.ToBoolean(row.Cells[0].Value);


if (isSelected)


// listRowId.Add(rr);
MessageBox.Show(rr.ToString() + " checked");




else
MessageBox.Show(rr.ToString() + " Unchecked");


rr++;


}

csharpdoost
شنبه 27 آذر 1400, 21:57 عصر
این ستون چک باکسها بصورت runtime به دیتاگرید اضافه میشه ،نمیدونم تاثیری داره یا اشکال از جای دیگه است.


if (!creatcheckbox)
{
creatcheckbox = true;
//Find the Location of Header Cell.
System.Drawing.Point headerCellLocation = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location;




//Place the Header CheckBox in the Location of the Header Cell.
headerCheckBox.Location = new System.Drawing.Point(headerCellLocation.X + 75, headerCellLocation.Y + 2);
headerCheckBox.BackColor = Color.White;
headerCheckBox.Size = new Size(18, 18);




//Assign Click event to the Header CheckBox.
headerCheckBox.Click += new EventHandler(HeaderCheckBox_Clicked);
dataGridView1.Controls.Add(headerCheckBox);


//Add a CheckBox Column to the DataGridView at the first position.
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "";
checkBoxColumn.Width = 30;
checkBoxColumn.Name = "checkBoxColumn";
dataGridView1.Columns.Insert(0, checkBoxColumn);


//Assign Click event to the DataGridView Cell.
dataGridView1.CellContentClick += new DataGridViewCellEventHandler(DataGridView_CellClic k);


}// end if

private void HeaderCheckBox_Clicked(object sender, EventArgs e)
{
// Necessary to end the edit mode of the Cell.
dataGridView1.EndEdit();


//Loop and check and uncheck all row CheckBoxes based on Header Cell CheckBox.
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell checkBox = (row.Cells["checkBoxColumn"] as DataGridViewCheckBoxCell);
checkBox.Value = headerCheckBox.Checked;
}
}




private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
//Loop to verify whether all row CheckBoxes are checked or not.
bool isChecked = true;
foreach (DataGridViewRow row in dataGridView1.Rows)
{


bool isSelected = Convert.ToBoolean(row.Cells["checkBoxColumn"].EditedFormattedValue);


if (isSelected)
{
isChecked = false;
break;
}


}


headerCheckBox.Checked = isChecked;


}

mazoolagh
دوشنبه 29 آذر 1400, 10:11 صبح
فکر کنم رخدادهای cellclick یا cellcontentclick قبل از این که مقدار cell آپدیت بشه رخ میدن و همین باعث عدم تشخیص مقدار درست میشه.

شاید بهتر باشه از رخداد cellvaluechanged استفاده کنین که در اون مطمئن هستیم مقدار آپدیت شده واقعی رو داریم.
گذشته از این، یک احتمال هم در نظر بگیرین که کاربر چک باکس رو نه با کلیک موس بلکه با کلید space تغییر بده.

در ضمن چک کنین که ویژگی tristate چک باکس ها هم true نباشه.