如何自动添加复选框到datagridview [英] How to automatic add checkbox to the datagridview

查看:57
本文介绍了如何自动添加复选框到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个使用datagridview的个性化日历,同时我想添加复选框控件(而不是datagridview列中的模板)。但我的问题是,当我成功插入复选框时,不显示checbox.Text值。给我一个想法吗?



这是我的snick代码:

private void Form1_Load(object sender,EventArgs e)< br $>
{

DrawCalendor(2013,9);



}

private void DrawCalendor(int year,int month)

{

DateTime time = new DateTime(year,month,14);



string [] WeekInfo = new string [] {Sunday,Monday,星期二,星期三,星期四,星期五,星期六};

foreach(WeekInfo中的字符串元素)

{

DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();



column.SortMode = DataGridViewColumnSortMode.NotSortable;

column.HeaderText = element;

dataGridView1.Columns.Add(column);









}

dataGridView1.Rows.Add(6);

int num = 0; // jishu

int week = Convert.ToInt16(DateTime.Parse(time.ToString (yyyy年MM月01日))。DayOfWeek);

int monthdays = DateTime.DaysInMonth(年,月);

尝试

$



for(int i = 0;我< dataGridView1.Rows.Count; i ++)

{





if(i> 0)

{

周= 0;

}

for(int j = week; j< dataGridView1.Columns.Count; j ++)

{

num ++;

if(num> monthdays)

{

break ;

}



CheckBox chk = new CheckBox();

chk.Name = num.ToString( );

chk.Size = new Size(15,15);

chk.Text = num.ToString();

chk。 TextAlign = ContentAlignment.MiddleCenter;

this.dataGridView1.Controls.Add(chk);

Rectangle reg = dataGridView1.GetCellDisplayRectangle(j,i,true);

chk.Left = reg.Left + 4;

chk.Top = reg.Top + 5;

}

}

}

catch(例外情况)

{

MessageBox.Show(ex.ToString());

}

}

I want a personalized calendar, using datagridview, at the same time I want to add checkbox controls (not the template in datagridview column). But my problem is that when I insert checkbox when successful, checbox.Text values ​​are not displayed. Give me an idea of ​​it?

this is my snick code:
private void Form1_Load(object sender, EventArgs e)
{
DrawCalendor(2013, 9);

}
private void DrawCalendor(int year, int month)
{
DateTime time = new DateTime(year, month, 14);

string[] WeekInfo = new string[] { "Sunday", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday " };
foreach (string element in WeekInfo)
{
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();

column.SortMode = DataGridViewColumnSortMode.NotSortable;
column.HeaderText = element;
dataGridView1.Columns.Add(column);




}
dataGridView1.Rows.Add(6);
int num = 0;//jishu
int week = Convert.ToInt16(DateTime.Parse(time.ToString("yyyy年MM月01日")).DayOfWeek);
int monthdays = DateTime.DaysInMonth(year, month);
try
{

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{


if (i > 0)
{
week = 0;
}
for (int j = week; j < dataGridView1.Columns.Count; j++)
{
num++;
if (num > monthdays)
{
break;
}

CheckBox chk = new CheckBox();
chk.Name = num.ToString();
chk.Size = new Size(15, 15);
chk.Text = num.ToString ();
chk.TextAlign = ContentAlignment.MiddleCenter;
this.dataGridView1.Controls.Add(chk);
Rectangle reg = dataGridView1.GetCellDisplayRectangle(j, i, true);
chk.Left = reg.Left+4;
chk.Top = reg.Top+5;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

推荐答案

string qry2 = "Select * from student";
           SqlCommand cmd2 = new SqlCommand(qry2, sconn);
           SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
           DataTable dt2 = new DataTable();
           da2.Fill(dt2);
           BindingSource bs2 = new BindingSource();
           bs2.DataSource = dt2;
           dataGridView2.DataSource = bs2;
           DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
           colCB.HeaderText = "Delete";
           colCB.ValueType = typeof(System.Boolean);
           colCB.TrueValue = "True";
           colCB.FalseValue = "False";
           dataGridView2.AutoGenerateColumns = false;
           dataGridView2.Columns.Insert(0, colCB);

           dataGridView2.Columns[0].Width = 40;


这篇关于如何自动添加复选框到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆