C# DataGridView相关操作
全选
private void SelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < MedicalList.Rows.Count; i++)
{
if ((Convert.ToBoolean(MedicalList.Rows[i].Cells[0].Value) == false))
{
MedicalList.Rows[i].Cells[0].Value = "True";
}
else
continue;
}
}
取消全选
private void UnSelectAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < MedicalList.Rows.Count; i++)
{
if ((Convert.ToBoolean(MedicalList.Rows[i].Cells[0].Value) == true))
{
MedicalList.Rows[i].Cells[0].Value = "False";
}
else
continue;
}
}
获取所有行数据
public static void UploadMedicalTh()
{
try
{
UploadMedicalT = true;
int k = 0, j = 0;
for (int i = 0; i < Convert.ToInt32(ThirdParty_Main.MedicalList.Rows.Count.ToString()); i++)
{
ThirdParty_Main.MedicalList.EndEdit();
if (!Convert.ToBoolean(((System.Windows.Forms.DataGridViewCheckBoxCell)ThirdParty_Main.MedicalList.Rows[i].Cells["选择"]).Value))
{
string out = ThirdParty_Main.MedicalList.Rows[i].Cells["内容"];
}
else
{
j++;
}
}
}
catch (Exception)
{
}
finally
{
UploadMedicalT = false;
}
}
DataGridView转DataTable
public static DataTable GetDgvToTable(DataGridView dgv)
{
DataTable dt = new DataTable();
for (int count = 0; count < dgv.Columns.Count; count++)
{
DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
dt.Columns.Add(dc);
}
for (int count = 0; count < dgv.Rows.Count; count++)
{
DataRow dr = dt.NewRow();
for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
{
dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
}
dt.Rows.Add(dr);
}
return dt;
}
动态关联关键字筛选
public static System.Data.DataTable UserListTable = new System.Data.DataTable();
private void Status_SelectedIndexChanged(object sender, EventArgs e)
{
if (Status.Text == "全部")
{
ThirdParty_Main.MedicalList.Rows.Clear();
foreach (System.Data.DataRow Lists in UserListTable.Rows)
{
int index = ThirdParty_Main.MedicalList.Rows.Add(new DataGridViewRow());
ThirdParty_Main.MedicalList.Rows[index].Cells["状态"].Value = Lists["状态"];
}
ThirdParty_Main.MedicalList.EnableHeadersVisualStyles = false;
ThirdParty_Main.MedicalList.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("黑体", 10, System.Drawing.FontStyle.Bold);
ThirdParty_Main.MedicalList.ColumnHeadersDefaultCellStyle.ForeColor = System.Drawing.Color.Purple;
}
else
{
System.Data.DataTable mydataTable = new System.Data.DataTable();
System.Data.DataTable dt = UserListTable;
DataView dv = dt.DefaultView;
dv.RowFilter = string.Format("状态 = '{0}'", Status.Text);
mydataTable = dv.ToTable(true);
ThirdParty_Main.MedicalList.Rows.Clear();
foreach (System.Data.DataRow Lists in mydataTable.Rows)
{
int index = ThirdParty_Main.MedicalList.Rows.Add(new DataGridViewRow());
ThirdParty_Main.MedicalList.Rows[index].Cells["状态"].Value = Lists["状态"];
}
ThirdParty_Main.MedicalList.EnableHeadersVisualStyles = false;
ThirdParty_Main.MedicalList.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("黑体", 10, System.Drawing.FontStyle.Bold);
ThirdParty_Main.MedicalList.ColumnHeadersDefaultCellStyle.ForeColor = System.Drawing.Color.Purple;
}
}
//网络素材仅限收藏 方便学习