Windows 窗体数据绑定 CheckedListBox

发布于 2024-11-15 12:40:25 字数 279 浏览 4 评论 0原文

我有 ASP 经验,但对 WinForms 完全陌生。我想做的是:我在 Form1 上有一个 CheckedListBox,它链接到链接到 SQL 数据库的绑定源。我有另一个表单(Form2),单击“添加”按钮后会弹出,然后他们可以添加记录。我希望发生的是,一旦他们在 Form 2 中添加记录,Form1 中的 CheckedListBox 也会更新。我尝试过 DataSource 和 DisplayMember 属性,但这似乎不起作用。有人能指出我正确的方向吗?

如果有人知道更好的方法来做到这一点,我也会洗耳恭听。谢谢。

I have experience with ASP, but completely new to WinForms. What I'm trying to do is this: I have a CheckedListBox on Form1 that is linked to a binding source that is linked to a SQL database. I have another Form (Form2) that pops up upon clicking on an "Add" button, and then they can add a record. What I would like to have happen is as soon as they Add the record in Form 2, the CheckedListBox in Form1 updates as well. I've tried DataSource and DisplayMember properties but that doesn't seem to do the trick. Can someone point me in the right direction?

If anyone knows of a better way to do this, I'm also all ears. Thanks.

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

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

发布评论

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

评论(3

吲‖鸣 2024-11-22 12:40:25

看看之前回答过的帖子

我指示该人使用第一个表单中的参数调用第二个表单...然后第二个表单可以直接使用它。您可以通过传递第一个表单本身并让它执行几乎任何操作来做到这一点......但以最简单的形式,允许您保留表单。然后,在第二个表单的单击/添加/保存/无论什么内容中,引用第一个表单并直接设置它(或通过公共属性 getter/setter 间接设置)。

Take a look at a previous answered post.

I directed the person to call the second form with a parameter from the first... Then the second form can utilize it directly. You could do this by passing in the first form itself and have it do almost anything... but in its simplest form, allow you to preserve the form. Then, in the click / add / save / whatever of the second form, reference the first form and set it directly (or indirectly via a public property getter/setter).

尴尬癌患者 2024-11-22 12:40:25

可以从 Form2 访问 Form1 吗?如果可以的话,最简单的方法是在按下的按钮中添加一行来检查复选框,

例如 form1.checkboxName.Checked = true;

如果您显示一些代码,将会有所帮助: )

Can you access Form1 from Form2? If you can the easiest way would be to just ad a line in button pressed that checks the check-box

something like form1.checkboxName.Checked = true;

it would help if you showed some of your code :)

饮湿 2024-11-22 12:40:25

我找到了我的问题的答案。我在form1上处理了所有事情。我创建了一个名为refreshData的方法,当我点击我的图标弹出form2时,我等待form2的Dialog Result OK,然后我调用了refreshData方法。希望这对其他人有帮助:

private void pictureBox1_Click(object sender, EventArgs e)
    {
        form2 box = new form2();
        using (box)
        {
            box.ShowDialog();
            if (box.DialogResult == DialogResult.OK)
            {
                refreshData();
            }
            box.Dispose();
        }
    }
    private void refreshData()
{
ADODB.Recordset rs = new ADODB.Recordset();
           ADODB.Connection adoCon = new ADODB.Connection();
           adoCon.Open("put Connection String Here");
           rs.Open("Put Select query Here",adoCon,ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockOptimistic);
           DataSet myDS = new DataSet();
           OleDbDataAdapter da = new OleDbDataAdapter();
           da.Fill(myDS, rs,"MyTable");
           chkList1.DataSource = null;
           chkList1.DataSource = myDS.Tables[0];
           chkList1.DisplayMember = "Put Field to Display in CheckList here";
}

I figured out the answer to my question. I handled everything on form1. I created a method called refreshData, and when I clicked my icon to pop up form2, I waited for the Dialog Result of form2 to be OK, then I called the refreshData method. Hope this helps someone else:

private void pictureBox1_Click(object sender, EventArgs e)
    {
        form2 box = new form2();
        using (box)
        {
            box.ShowDialog();
            if (box.DialogResult == DialogResult.OK)
            {
                refreshData();
            }
            box.Dispose();
        }
    }
    private void refreshData()
{
ADODB.Recordset rs = new ADODB.Recordset();
           ADODB.Connection adoCon = new ADODB.Connection();
           adoCon.Open("put Connection String Here");
           rs.Open("Put Select query Here",adoCon,ADODB.CursorTypeEnum.adOpenStatic,ADODB.LockTypeEnum.adLockOptimistic);
           DataSet myDS = new DataSet();
           OleDbDataAdapter da = new OleDbDataAdapter();
           da.Fill(myDS, rs,"MyTable");
           chkList1.DataSource = null;
           chkList1.DataSource = myDS.Tables[0];
           chkList1.DisplayMember = "Put Field to Display in CheckList here";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文