tags:

views:

17

answers:

1

OK, so i've got a DataGridView for some Project Management Software, i want to be able to double click a row it opens a form and fills in the fields and combo boxes with the relvant data, so that i can change what ever needs to be changed. In this case the most important thing is changing the Status. As in the Project Management Software i have bugs and tasks, that have a status and that needs to be changed at some point obviously when the Bug has been resolved or the task completed.

So i will show how the DGV is getting data for the bugs section.

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;
        }

As you can see my approach is fairly simplistic. I will also show the code for creating a new Bug. The ComboBoxes have been DataBound using the GUI option as opposed to me actually hardcoding it in.

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();
        }

Now I know there a few bits of sloppy code in there, but i am new to C# and i personally think im not doing too badly. But if you can see anything that should be improved please feel free to mentiond that as well.

My DataClasses1DataContext is organised so that there are Associations between the UserID, PriorityID, CategoryID, UserID, ProjectID and StatusID.

So basically i want to be able to click on Bug1 (an example bug) in the DGV and that needs to open up a form (that i havnt made yet) and i need to be able to edit every piece of data in that Bug.

If you require anymore information I'll be more than happy to supply it!

Thanks,

Brendan

A: 

Hi,

The DataGridView will have a CellDoubleClick event. You can use this to respond to the UI, however don't forget you need to ignore invalid row or column indexes (-1s). I believe these crop in if people click on the row/column headers.

You the event args to get the row index and then the row. From the row you can access the DataBoundItem property. This should be directly cast-able to Bug.

You can then pass this reference to your new form, bind it to the controls and make your edits. Because its a reference, it will update the data on the other form too - though you may have to refresh your grid.

You can then decide whether to update the database on the details form or the master form (I'd personally make saving part of the grid form).

Adam