如何将日期Cell DataGridView的值转到DateTimePicker中?

发布于 2025-01-28 18:23:23 字数 323 浏览 2 评论 0原文

第一件事转到DataGridView事件Mouseclick,并写下以下代码:

DateTime dateValueOftheCell = DateTime.Parse(dataGridView1.CurrentRow.Cells[4].Value.ToString());
dateTimePicker1.Value = dateValueOftheCell.Date;

在我的情况下,我的datagridview在第5列中具有date值,即索引4

索引从0开始,索引值=列数-1 5-1 = 4

First thing go to datagridview event MouseClick, and write the code below:

DateTime dateValueOftheCell = DateTime.Parse(dataGridView1.CurrentRow.Cells[4].Value.ToString());
dateTimePicker1.Value = dateValueOftheCell.Date;

in my case my datagridview has the date value in the 5th column which is the index 4

index start from 0, index value = number of columns - 1
5 - 1 = 4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜灵血窟げ 2025-02-04 18:23:24

您应该将DataGridView DataSource属性设置为BindingSource,该属性可以轻松访问当前行数据。另外,您可以索引到bindingsource,以获取特定行,如下所示,很难编码以获取第三行数据。

public partial class Form2 : Form
{
    private readonly BindingSource _bindingSource = new BindingSource();
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("BirthDate", typeof(DateTime));
        dt.Rows.Add(1, new DateTime(2020, 1, 1));
        dt.Rows.Add(2, new DateTime(1978, 9, 11));
        dt.Rows.Add(3, new DateTime(1987, 3, 2));

        _bindingSource.DataSource = dt;

        dataGridView1.DataSource = _bindingSource;
        dateTimePicker1.DataBindings.Add("Value", _bindingSource, "BirthDate");
    }

    private void GetButton_Click(object sender, EventArgs e)
    {
        var row = ((DataRowView)_bindingSource[2]).Row;
        MessageBox.Show($@"Birth is {row.Field<DateTime>("BirthDate"):d}");
    }
}

You should setup the DataGridView DataSource property to a BindingSource which allows easy access to the current row data. Also you can index into the BindingSource to get a specific row as shown below which is hard coded to get the third row data.

public partial class Form2 : Form
{
    private readonly BindingSource _bindingSource = new BindingSource();
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("BirthDate", typeof(DateTime));
        dt.Rows.Add(1, new DateTime(2020, 1, 1));
        dt.Rows.Add(2, new DateTime(1978, 9, 11));
        dt.Rows.Add(3, new DateTime(1987, 3, 2));

        _bindingSource.DataSource = dt;

        dataGridView1.DataSource = _bindingSource;
        dateTimePicker1.DataBindings.Add("Value", _bindingSource, "BirthDate");
    }

    private void GetButton_Click(object sender, EventArgs e)
    {
        var row = ((DataRowView)_bindingSource[2]).Row;
        MessageBox.Show($@"Birth is {row.Field<DateTime>("BirthDate"):d}");
    }
}

enter image description here

情深如许 2025-02-04 18:23:24

使用LINQ,我为鼠标单击事件创建了一个简单的代码。

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        int idx = dataGridView1.CurrentRow.Index;

        if (dataGridView1.Rows[idx].Cells[1].Value.ToString() != "") //Cells[1] = datetime column index
        {
            dateTimePicker1.Value = (DateTime)dataGridView1.Rows.Cast<DataGridViewRow>().ToList().Where(s => s.Index == idx && s.Cells[1].Value != null).Select(s => s.Cells[1].Value).FirstOrDefault();
        }
    }

我希望它有帮助!

Using LINQ, I've created a simple code for Mouse Click event.

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        int idx = dataGridView1.CurrentRow.Index;

        if (dataGridView1.Rows[idx].Cells[1].Value.ToString() != "") //Cells[1] = datetime column index
        {
            dateTimePicker1.Value = (DateTime)dataGridView1.Rows.Cast<DataGridViewRow>().ToList().Where(s => s.Index == idx && s.Cells[1].Value != null).Select(s => s.Cells[1].Value).FirstOrDefault();
        }
    }

I hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文