好的,所以我有一个DataGridView用于某些项目管理软件,我希望能够双击它打开一个表单的行,并用relvant数据填充字段和组合框,这样我就可以更改什么需要改变。在这种情况下,最重要的是改变状态。与项目管理软件一样,我有错误和任务,这些错误和任务具有状态,并且在Bug已经解决或任务完成时显然需要在某些时候进行更改。
因此,我将展示DGV如何获取错误部分的数据:
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使用GUI选项是DataBound,而不是我实际上硬编码。
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#的新手,我个人认为我做得不是很糟糕。但如果你能看到任何应该改进的东西,请随意提及。
我的DataClasses1DataContext是有条理的,因此UserID,PriorityID,CategoryID,UserID,ProjectID和StatusID之间存在关联。
所以基本上我希望能够点击DGV中的Bug1(一个示例错误)并且需要打开一个表单(我已经制作了)并且我需要能够编辑每个数据片段那个Bug。
如果您需要更多信息,我将非常乐意提供这些信息!
谢谢,
布伦丹
答案 0 :(得分:0)
DataGridView将有CellDoubleClick
个事件。您可以使用它来响应UI,但是不要忘记您需要忽略无效的行或列索引(-1s)。如果人们点击行/列标题,我相信这些就会收获。
你是事件args获取行索引然后是行。从该行您可以访问DataBoundItem
属性。这应该可以直接转换为Bug
。
然后,您可以将此引用传递给新表单,将其绑定到控件并进行编辑。因为它是一个引用,它也会更新另一个表单上的数据 - 尽管你可能需要刷新你的网格。
然后,您可以决定是在更新详细信息表单还是主表单上更新数据库(我个人会保存网格表单的一部分)。