首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在DataGridView中打开要编辑的行,DGV在C#中有SQL后端

在DataGridView中打开要编辑的行,DGV在C#中有SQL后端
EN

Stack Overflow用户
提问于 2010-07-03 13:48:17
回答 1查看 278关注 0票数 0

好的,我已经有了一些项目管理软件的DataGridView,我想要能够双击一行,它打开一个表单,并用相关数据填充字段和组合框,这样我就可以更改任何需要更改的内容。在这种情况下,最重要的事情是更改状态。就像在项目管理软件中一样,我有一些Bug和任务,它们有一个状态,当Bug已经解决或任务完成时,显然需要在某个点上进行更改。

因此,我将展示DGV是如何为bug部分获取数据的:

代码语言:javascript
复制
public void ViewAllBugs()
        {
            DataClasses1DataContext dc = new DataClasses1DataContext(sConnectionString);

            var Bugs =
                (from data in dc.GetTable<Bug>()
                 from user in dc.GetTable<User>()
                 from projects in dc.GetTable<Project>()
                 from priority in dc.GetTable<Priority>()
                 from status in dc.GetTable<Status>()
                 from category in dc.GetTable<Category>()
                 where data.UserID == user.UserID && data.ProjectID == projects.ProjectID && data.PriorityID == priority.PriorityID && status.StatusID == data.StatusID && category.CategoryID == data.CategoryID
                 select new
                 {
                     Bug = data.Name,
                     Project = projects.Name,
                     Priority = priority.Priority1,
                     Status = status.Name,
                     Category = category.Category1,
                     Assigned_To = user.Username,
                     Start = data.Opened_Date,
                     End = data.Closed_Date,
                     Description = data.Description,
                 });

            dgvBugs.DataSource = Bugs;
        }

正如您所看到的,我的方法相当简单。我还将展示创建新Bug的代码。ComboBoxes已经使用图形用户界面选项进行了DataBound,而不是我实际将其硬编码到其中。

代码语言:javascript
复制
public void CreateBug()
        {
            string sBugName = txtName.Text;
            string sDescription = txtDescription.Text;
            int iProject = Convert.ToInt32(cbProject.SelectedValue);
            int iPriority = Convert.ToInt32(cbPriority.SelectedValue);
            int iStatus = Convert.ToInt32(cbStatus.SelectedValue);
            int iCategory = Convert.ToInt32(cbCategory.SelectedValue);
            int iUser = Convert.ToInt32(cbAssigned.SelectedValue);
            DateTime dtOpen = dateOpened.Value;
            DateTime dtClosed = dateClosed.Value;

            DataClasses1DataContext dc = new DataClasses1DataContext(sConnectionString);

            Table<Bug> tBug = dc.GetTable<Bug>();
            Bug nBug = new Bug();

            nBug.Name = sBugName;
            nBug.ProjectID = iProject;
            nBug.PriorityID = iPriority;
            nBug.StatusID = iStatus;
            nBug.CategoryID = iCategory;
            nBug.UserID = iUser;
            nBug.Opened_Date = dtOpen;
            nBug.Closed_Date = dtClosed;
            nBug.Description = sDescription;

            tBug.InsertOnSubmit(nBug);
            dc.SubmitChanges();

            this.Close();
        }

现在我知道其中有一些草率的代码,但我是C#的新手,我个人认为我做得还不错。但是,如果您能看到任何需要改进的地方,也请随时提出来。

My DataClasses1DataContext的组织方式是在UserID、PriorityID、CategoryID、UserID、ProjectID和StatusID之间建立关联。

所以基本上,我希望能够在DGV中点击Bug1 (一个示例错误),这需要打开一个表单(我还没有创建),我需要能够编辑该错误中的每一条数据。

如果您需要更多信息,我将非常乐意提供!

谢谢,

布兰登

EN

回答 1

Stack Overflow用户

发布于 2010-07-03 14:41:50

DataGridView将有一个CellDoubleClick事件。您可以使用它来响应UI,但是不要忘记您需要忽略无效的行或列索引(-1s)。我相信如果人们点击行/列标题,这些内容就会被裁剪。

您可以使用事件参数来获取行索引,然后获取行。在该行中,您可以访问DataBoundItem属性。这应该可以直接转换为Bug

然后,您可以将此引用传递给新窗体,将其绑定到控件并进行编辑。因为它是一个引用,所以它也会更新另一个表单上的数据-尽管你可能需要刷新你的网格。

然后,您可以决定是更新详细信息表单上的数据库还是主表单上的数据库(我个人认为保存是网格表单的一部分)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3170455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档