.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

Delete Row From datagrid view

Posted By: kamlesh     Posted Date: February 01, 2011    Points:2   Category :Windows Application
hi

i want to Delete Row From data grid view in windows application.

thanks
Kamlesh nikam

Responses
Author: Blessy Baby             
Posted Date: February 01, 2011     Points: 5   

Hi,

I think that you can use

DataGridView1.Rows.RemoveAt[index of the row]
DataGridView1.Rows.Remove [object of that row]

Blessy Baby
Project Lead
Digitalmesh Softech India (p) Limited
Cochin,Kerala
Author: shankey             
Posted Date: February 01, 2011     Points: 5   
Author: kamlesh             
Posted Date: February 03, 2011     Points: 5   


hi thanks but ihave use folowing code

try
{
foreach (DataGridViewRow dr in dtgMail.SelectedRows)
{
Int32 rowToDelete = this.dtgMail.Rows.GetFirstRow(DataGridViewElementStates.Selected);
if (rowToDelete > -1)
{
this.dtgMail.Rows.RemoveAt(rowToDelete);
}
}
}
catch (Exception ex)
{

throw ex;
}


I got error in the code.....




An unhandled exception of type 'System.InvalidOperationException' occurred in O

Additional information: Rows cannot be programmatically removed unless the DataGridView is data-bound to an IBindingList that supports change notification and allows deletion.

Thanks And Regards.
Kamlesh Nikam
Author: krishana singh             
Posted Date: February 16, 2011     Points: 5   

using datagridview delete and edit row properties

it is use from

add field

Author: kamlesh             
Posted Date: March 17, 2011     Points: 20   

private void restoreToLiveExchangeServerToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (TreeMailNodes.SelectedNode != null)
{
string NodeName = "";
string strNodes = TreeMailNodes.SelectedNode.FullPath;
String[] Nodes = strNodes.Split('\\');
//Nodes.Remove(0);

for (int i = 1; i < Nodes.Length; i++)
{

NodeName = NodeName + Nodes[i];
if (i != Nodes.Length - 1)
NodeName = NodeName + "\\";

}


string RouteFolderName = MailService.GetTopFolderBySubFolderId(Convert.ToInt64(TreeMailNodes.SelectedNode.Tag));

clsExport Export = new clsExport();
Export.UserCount = 1;
List<ClsExportUserMetaData> userList = new List<ClsExportUserMetaData>();
ClsExportUserMetaData user = new ClsExportUserMetaData();
user.ExportType = ExportTypeEnum.Selected;
user.UserName = RouteFolderName;
List<clsExportFolder> folderList = new List<clsExportFolder>();
clsExportFolder folder1 = new clsExportFolder();
folder1.ExportType = ExportTypeEnum.Selected;
folder1.FolderName = NodeName;
List<clsExportMail> MailList = new List<clsExportMail>();
List<clsExportTask> TaskList = new List<clsExportTask>();
List<ClsExportContact> ContactList = new List<ClsExportContact>();
List<ClsExportJournal> JournalList = new List<ClsExportJournal>();
List<ClsExportCalendar> CalendarList = new List<ClsExportCalendar>();
List<ClsExportNote> NoteList = new List<ClsExportNote>();

// DataGridViewRow[] RowList = dtgMail.SelectedRows;

for (int i = 0; i < dtgMail.Rows.Count; i++)
{
if (dtgMail.Rows[i].Selected == true)
{
clsExportMail Mail1 = new clsExportMail();
Mail1.SentDate = Convert.ToDateTime(dtgMail.Rows[i].Cells["MailDate"].Value);
//Mail1.SentDate = Mail1.SentDate.ToLocalTime();
//long mailDate = Mail1.SentDate.ToFileTimeUtc();

//TimeZone tz=TimeZone.CurrentTimeZone;
//Mail1.SentDate = tz.ToLocalTime(Mail1.SentDate);

Mail1.Subject = Convert.ToString(dtgMail.Rows[i].Cells["MailSubject"].Value);
MailList.Add(Mail1);
}
}

for (int i = 0; i < dtgTask.Rows.Count; i++)
{
if (dtgTask.Rows[i].Selected == true)
{
clsExportTask task1 = new clsExportTask();
task1.SentDate = Convert.ToDateTime(dtgTask.Rows[i].Cells["CreationTime"].Value);
//task1.SentDate = task1.SentDate.ToLocalTime();
task1.Subject = Convert.ToString(dtgTask.Rows[i].Cells["Subject"].Value);
TaskList.Add(task1);
}
}

for (int i = 0; i < dtgContact.Rows.Count; i++)
{
if (dtgContact.Rows[i].Selected == true)
{
ClsExportContact cont1 = new ClsExportContact();
cont1.SentDate = Convert.ToDateTime(dtgContact.Rows[i].Cells["CreationTime"].Value);
//cont1.SentDate = cont1.SentDate.ToLocalTime();
//cont1.SentDate = Convert.ToString(dtgContact.Rows[i].Cells["Email"].Value);
cont1.Name = Convert.ToString(dtgContact.Rows[i].Cells["FullName"].Value);
ContactList.Add(cont1);
}
}


for (int i = 0; i < dtgJournal.Rows.Count; i++)
{
if (dtgJournal.Rows[i].Selected == true)
{

ClsExportJournal jour1 = new ClsExportJournal();
jour1.SentDate = Convert.ToDateTime(dtgJournal.Rows[i].Cells["CreationTime"].Value);
//jour1.SentDate = jour1.SentDate.ToLocalTime();
jour1.Subject = Convert.ToString(dtgJournal.Rows[i].Cells["Subject"].Value);

JournalList.Add(jour1);
}
}

for (int i = 0; i < dtgCalander.Rows.Count; i++)
{
if (dtgCalander.Rows[i].Selected == true)
{

ClsExportCalendar cale1 = new ClsExportCalendar();
cale1.SentDate = Convert.ToDateTime(dtgCalander.Rows[i].Cells["CreationTime"].Value);
//cale1.SentDate = cale1.SentDate.ToLocalTime();
cale1.Subject = Convert.ToString(dtgCalander.Rows[i].Cells["Subject"].Value);
CalendarList.Add(cale1);
}
}


for (int i = 0; i < dtgNote.Rows.Count; i++)
{
if (dtgNote.Rows[i].Selected == true)
{

ClsExportNote not1 = new ClsExportNote();
not1.SentDate = Convert.ToDateTime(dtgNote.Rows[i].Cells["CreationTime"].Value);
//not1.SentDate = not1.SentDate.ToLocalTime();
not1.Subject = Convert.ToString(dtgNote.Rows[i].Cells["Subject"].Value);
NoteList.Add(not1);
}
}



folder1.MailList = MailList;
folder1.TaskList = TaskList;
folder1.ContactList = ContactList;
folder1.JournalList = JournalList;
folder1.CalendarList = CalendarList;
folder1.NoteList = NoteList;



folderList.Add(folder1);

user.FolderList = folderList;
userList.Add(user);
Export.UserList = userList;
ClsExportCreateXML XMLCreater = new ClsExportCreateXML();
XMLCreater.CreateXML(Export);


} //MessageBox.Show("Live Exchange Server");

CLICaller.bIsPublicFolderRestore = false;

//CLICaller.bIsPublicFolderRestore = true;
FrmRestore frmrest = new FrmRestore();
frmrest.ShowDialog(this);
}


private void restoreToPublicFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
if (TreeMailNodes.SelectedNode != null)
{
string NodeName = "";
string strNodes = TreeMailNodes.SelectedNode.FullPath;
String[] Nodes = strNodes.Split('\\');
//Nodes.Remove(0);

for (int i = 1; i < Nodes.Length; i++)
{

NodeName = NodeName + Nodes[i];
if (i != Nodes.Length - 1)
NodeName = NodeName + "\\";

}


string RouteFolderName = MailService.GetTopFolderBySubFolderId(Convert.ToInt64(TreeMailNodes.SelectedNode.Tag));

clsExport Export = new clsExport();
Export.UserCount = 1;
List<ClsExportUserMetaData> userList = new List<ClsExportUserMetaData>();
ClsExportUserMetaData user = new ClsExportUserMetaData();
user.ExportType = ExportTypeEnum.Selected;
user.UserName = RouteFolderName;
List<clsExportFolder> folderList = new List<clsExportFolder>();
clsExportFolder folder1 = new clsExportFolder();
folder1.ExportType = ExportTypeEnum.Selected;
folder1.FolderName = NodeName;
List<clsExportMail> MailList = new List<clsExportMail>();
List<clsExportTask> TaskList = new List<clsExportTask>();
List<ClsExportContact> ContactList = new List<ClsExportContact>();
List<ClsExportJournal> JournalList = new List<ClsExportJournal>();
List<ClsExportCalendar> CalendarList = new List<ClsExportCalendar>();
List<ClsExportNote> NoteList = new List<ClsExportNote>();

// DataGridViewRow[] RowList = dtgMail.SelectedRows;

for (int i = 0; i < dtgMail.Rows.Count; i++)
{
if (dtgMail.Rows[i].Selected == true)
{
clsExportMail Mail1 = new clsExportMail();
Mail1.SentDate = Convert.ToDateTime(dtgMail.Rows[i].Cells["MailDate"].Value);
Mail1.Subject = Convert.ToString(dtgMail.Rows[i].Cells["MailSubject"].Value);
MailList.Add(Mail1);
}
}

for (int i = 0; i < dtgTask.Rows.Count; i++)
{
if (dtgTask.Rows[i].Selected == true)
{
clsExportTask task1 = new clsExportTask();
task1.SentDate = Convert.ToDateTime(dtgTask.Rows[i].Cells["CreationTime"].Value);
task1.Subject = Convert.ToString(dtgTask.Rows[i].Cells["Subject"].Value);
TaskList.Add(task1);
}
}

for (int i = 0; i < dtgContact.Rows.Count; i++)
{
if (dtgContact.Rows[i].Selected == true)
{
ClsExportContact cont1 = new ClsExportContact();
cont1.SentDate = Convert.ToDateTime(dtgContact.Rows[i].Cells["CreationTime"].Value);
//cont1.SentDate = Convert.ToString(dtgContact.Rows[i].Cells["Email"].Value);
cont1.Name = Convert.ToString(dtgContact.Rows[i].Cells["FullName"].Value);
ContactList.Add(cont1);
}
}


for (int i = 0; i < dtgJournal.Rows.Count; i++)
{
if (dtgJournal.Rows[i].Selected == true)
{

ClsExportJournal jour1 = new ClsExportJournal();
jour1.SentDate = Convert.ToDateTime(dtgJournal.Rows[i].Cells["CreationTime"].Value);
jour1.Subject = Convert.ToString(dtgJournal.Rows[i].Cells["Subject"].Value);
JournalList.Add(jour1);
}
}

for (int i = 0; i < dtgCalander.Rows.Count; i++)
{
if (dtgCalander.Rows[i].Selected == true)
{

ClsExportCalendar cale1 = new ClsExportCalendar();
cale1.SentDate = Convert.ToDateTime(dtgCalander.Rows[i].Cells["CreationTime"].Value);
cale1.Subject = Convert.ToString(dtgCalander.Rows[i].Cells["Subject"].Value);
CalendarList.Add(cale1);
}
}


for (int i = 0; i < dtgNote.Rows.Count; i++)
{
if (dtgNote.Rows[i].Selected == true)
{

ClsExportNote not1 = new ClsExportNote();
not1.SentDate = Convert.ToDateTime(dtgNote.Rows[i].Cells["CreationTime"].Value);
not1.Subject = Convert.ToString(dtgNote.Rows[i].Cells["Subject"].Value);
NoteList.Add(not1);
}
}



folder1.MailList = MailList;
folder1.TaskList = TaskList;
folder1.ContactList = ContactList;
folder1.JournalList = JournalList;
folder1.CalendarList = CalendarList;
folder1.NoteList = NoteList;



folderList.Add(folder1);

user.FolderList = folderList;
userList.Add(user);
Export.UserList = userList;
ClsExportCreateXML XMLCreater = new ClsExportCreateXML();
XMLCreater.CreateXML(Export);


//MessageBox.Show("Live Exchange Server");

}
CLICaller.bIsPublicFolderRestore = true;

FrmRestore frmrest = new FrmRestore();
frmrest.ShowDialog(this);

}
private void restoreToPSTToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (TreeMailNodes.SelectedNode != null)
{
string NodeName = "";
string strNodes = TreeMailNodes.SelectedNode.FullPath;
String[] Nodes = strNodes.Split('\\');
//Nodes.Remove(0);

for (int i = 1; i < Nodes.Length; i++)
{

NodeName = NodeName + Nodes[i];
if (i != Nodes.Length - 1)
NodeName = NodeName + "\\";

}


string RouteFolderName = MailService.GetTopFolderBySubFolderId(Convert.ToInt64(TreeMailNodes.SelectedNode.Tag));

clsExport Export = new clsExport();
Export.UserCount = 1;
List<ClsExportUserMetaData> userList = new List<ClsExportUserMetaData>();
ClsExportUserMetaData user = new ClsExportUserMetaData();
user.ExportType = ExportTypeEnum.Selected;
user.UserName = RouteFolderName;
List<clsExportFolder> folderList = new List<clsExportFolder>();
clsExportFolder folder1 = new clsExportFolder();
folder1.ExportType = ExportTypeEnum.Selected;
folder1.FolderName = NodeName;
List<clsExportMail> MailList = new List<clsExportMail>();
List<clsExportTask> TaskList = new List<clsExportTask>();
List<ClsExportContact> ContactList = new List<ClsExportContact>();
List<ClsExportJournal> JournalList = new List<ClsExportJournal>();
List<ClsExportCalendar> CalendarList = new List<ClsExportCalendar>();
List<ClsExportNote> NoteList = new List<ClsExportNote>();

// DataGridViewRow[] RowList = dtgMail.SelectedRows;

for (int i = 0; i < dtgMail.Rows.Count; i++)
{
if (dtgMail.Rows[i].Selected == true)
{
clsExportMail Mail1 = new clsExportMail();
Mail1.SentDate = Convert.ToDateTime(dtgMail.Rows[i].Cells["MailDate"].Value);
Mail1.Subject = Convert.ToString(dtgMail.Rows[i].Cells["MailSubject"].Value);
MailList.Add(Mail1);
}
}

for (int i = 0; i < dtgTask.Rows.Count; i++)
{
if (dtgTask.Rows[i].Selected == true)
{
clsExportTask task1 = new clsExportTask();
task1.SentDate = Convert.ToDateTime(dtgTask.Rows[i].Cells["CreationTime"].Value);
task1.Subject = Convert.ToString(dtgTask.Rows[i].Cells["Subject"].Value);
TaskList.Add(task1);
}
}

for (int i = 0; i < dtgContact.Rows.Count; i++)
{
if (dtgContact.Rows[i].Selected == true)
{
ClsExportContact cont1 = new ClsExportContact();
cont1.SentDate = Convert.ToDateTime(dtgContact.Rows[i].Cells["CreationTime"].Value);
//cont1.SentDate = Convert.ToString(dtgContact.Rows[i].Cells["Email"].Value);
cont1.Name = Convert.ToString(dtgContact.Rows[i].Cells["FullName"].Value);
ContactList.Add(cont1);
}
}


for (int i = 0; i < dtgJournal.Rows.Count; i++)
{
if (dtgJournal.Rows[i].Selected == true)
{

ClsExportJournal jour1 = new ClsExportJournal();
jour1.SentDate = Convert.ToDateTime(dtgJournal.Rows[i].Cells["CreationTime"].Value);
jour1.Subject = Convert.ToString(dtgJournal.Rows[i].Cells["Subject"].Value);
JournalList.Add(jour1);
}
}

for (int i = 0; i < dtgCalander.Rows.Count; i++)
{
if (dtgCalander.Rows[i].Selected == true)
{

ClsExportCalendar cale1 = new ClsExportCalendar();
cale1.SentDate = Convert.ToDateTime(dtgCalander.Rows[i].Cells["CreationTime"].Value);
cale1.Subject = Convert.ToString(dtgCalander.Rows[i].Cells["Subject"].Value);
CalendarList.Add(cale1);
}
}


for (int i = 0; i < dtgNote.Rows.Count; i++)
{
if (dtgNote.Rows[i].Selected == true)
{

ClsExportNote not1 = new ClsExportNote();
not1.SentDate = Convert.ToDateTime(dtgNote.Rows[i].Cells["CreationTime"].Value);
not1.Subject = Convert.ToString(dtgNote.Rows[i].Cells["Subject"].Value);
NoteList.Add(not1);
}
}



folder1.MailList = MailList;
folder1.TaskList = TaskList;
folder1.ContactList = ContactList;
folder1.JournalList = JournalList;
folder1.CalendarList = CalendarList;
folder1.NoteList = NoteList;



folderList.Add(folder1);

user.FolderList = folderList;
userList.Add(user);
Export.UserList = userList;
ClsExportCreateXML XMLCreater = new ClsExportCreateXML();
XMLCreater.CreateXML(Export);


//MessageBox.Show("Live Exchange Server");
}
CLICaller.bIsPublicFolderRestore = false;
// Create new SaveFileDialog object
SaveFileDialog DialogSave = new SaveFileDialog();




DialogSave.DefaultExt = "pst";


DialogSave.Filter = "Personal Folder (*.pst)|*.pst";

// Adds a extension if the user does not
DialogSave.AddExtension = true;

// Restores the selected directory, next time
DialogSave.RestoreDirectory = true;

// Dialog title
DialogSave.Title = "Where do you want to save the file?";

// Startup directory
DialogSave.InitialDirectory = @"C:/";


// Show the dialog and process the result
if (DialogSave.ShowDialog() == DialogResult.OK)
{

CLICaller.RestoreDataToUser("", DialogSave.FileName, System.Environment.CurrentDirectory + "\\ExportMail.xml", "");

MessageBox.Show("Restore Completed Successfully", "Restore", MessageBoxButtons.OK, MessageBoxIcon.None);

}
else
{
//MessageBox.Show("You hit cancel or closed the dialog.");
DialogSave.Dispose();

}

DialogSave.Dispose();
DialogSave = null;
}


private void restoreToCMLToolStripMenuItem_Click(object sender, EventArgs e)
{
string NodeName = "";
string strNodes = TreeMailNodes.SelectedNode.FullPath;
String[] Nodes = strNodes.Split('\\');
//Nodes.Remove(0);

for (int i = 1; i < Nodes.Length; i++)
{

NodeName = NodeName + Nodes[i];
if (i != Nodes.Length - 1)
NodeName = NodeName + "\\";

}

FolderBrowserDialog browsefolder = new FolderBrowserDialog();


if (browsefolder.ShowDialog() == DialogResult.OK)
{
string RouteFolderName = MailService.GetTopFolderBySubFolderId(Convert.ToInt64(TreeMailNodes.SelectedNode.Tag));
ClsExportXML ExportXML = new ClsExportXML();
ExportXML.UserCountXML = 1;
List<ClsExportUserMetaDataForXML> UserListXML = new List<ClsExportUserMetaDataForXML>();
ClsExportUserMetaDataForXML UserXML = new ClsExportUserMetaDataForXML();
UserXML.ExportTypeXML = ExportTypeEnumXML.Selected;
UserXML.UserNameXML = RouteFolderName;
List<ClsExportFolderXML> folderListXML = new List<ClsExportFolderXML>();
ClsExportFolderXML folderXML1 = new ClsExportFolderXML();
folderXML1.ExportTypeXML = ExportTypeEnumXML.Selected;
folderXML1.FolderNameXML = NodeName;
List<ClsExportMailXML> MailXMLList = new List<ClsExportMailXML>();
List<ClsExportTaskXML> TaskXMLList = new List<ClsExportTaskXML>();
List<ClsExportContactXML> ContactXMLList = new List<ClsExportContactXML>();
List<ClsExportJournalXML> JournalXMLList = new List<ClsExportJournalXML>();
List<ClsExportCalendarXML> CalendarXMLList = new List<ClsExportCalendarXML>();
List<ClsExportNotesXML> NoteXMLList = new List<ClsExportNotesXML>();

// DataGridViewRow[] RowList = dtgMail.SelectedRows;

// Mail

if (dtgTask.Rows.Count > 0)
{

// Task
for (int i = 0; i < dtgTask.Rows.Count; i++)
{
if (dtgTask.Rows[i].Selected == true)
{
ClsExportTaskXML taskXML1 = new ClsExportTaskXML();
taskXML1.SentDate = Convert.ToDateTime(dtgTask.Rows[i].Cells["StartDate"].Value);
taskXML1.Subject = Convert.ToString(dtgTask.Rows[i].Cells["Subject"].Value);
taskXML1.Status = Convert.ToString(dtgTask.Rows[i].Cells["Status"].Value);
taskXML1.Body = Convert.ToString(dtgTask.Rows[i].Cells["Body"].Value);
taskXML1.DueDate = Convert.ToDateTime(dtgTask.Rows[i].Cells["DueDate"].Value);
taskXML1.PercentComplete = Convert.ToInt32(dtgTask.Rows[i].Cells["PercentComplete"].Value);
TaskXMLList.Add(taskXML1);

folderXML1.TaskListXML = TaskXMLList;
folderListXML.Add(folderXML1);
UserXML.FolderListXML = folderListXML;
UserListXML.Add(UserXML);
ExportXML.UserListXML = UserListXML;

ClsExportCreateXMLForXML XMLCreaterXML = new ClsExportCreateXMLForXML();
XMLCreaterXML.CreateXMLForXML(ExportXML, browsefolder.SelectedPath);

TaskXMLList.Clear();
folderListXML.Clear();
UserListXML.Clear();
}
}
dtgTask.ClearSelection();

}
if (dtgContact.Rows.Count > 0)
// Contact
{
for (int i = 0; i < dtgContact.Rows.Count; i++)
{
if (dtgContact.Rows[i].Selected == true)
{
ClsExportContactXML contXML1 = new ClsExportContactXML();
// contXML1.SentDate = Convert.ToDateTime(dtgContact.Rows[i].Cells["CreationTime"].Value);
contXML1.EmailID = Convert.ToString(dtgContact.Rows[i].Cells["Email1Address"].Value);
contXML1.Name = Convert.ToString(dtgContact.Rows[i].Cells["FullName"].Value);
contXML1.Companies = Convert.ToString(dtgContact.Rows[i].Cells["Companies"].Value);
contXML1.BusinessAddress = Convert.ToString(dtgContact.Rows[i].Cells["BusinessAddress"].Value);
contXML1.Mobile = Convert.ToString(dtgContact.Rows[i].Cells["MobileTelephoneNumber"].Value);
ContactXMLList.Add(contXML1);


folderXML1.ContactListXML = ContactXMLList;
folderListXML.Add(folderXML1);
UserXML.FolderListXML = folderListXML;
UserListXML.Add(UserXML);
ExportXML.UserListXML = UserListXML;
ClsExportCreateXMLForXML XMLCreaterXML = new ClsExportCreateXMLForXML();
XMLCreaterXML.CreateXMLForXML(ExportXML, browsefolder.SelectedPath);
ContactXMLList.Clear();
folderListXML.Clear();
UserListXML.Clear();
}
}

dtgContact.ClearSelection();


}

// Journal
if (dtgJournal.Rows.Count > 0)
{
for (int i = 0; i < dtgJournal.Rows.Count; i++)
{
if (dtgJournal.Rows[i].Selected == true)
{

ClsExportJournalXML jourXML1 = new ClsExportJournalXML();
jourXML1.SentDate = Convert.ToDateTime(dtgJournal.Rows[i].Cells["Start"].Value);
jourXML1.Subject = Convert.ToString(dtgJournal.Rows[i].Cells["Subject"].Value);
jourXML1.Companies = Convert.ToString(dtgJournal.Rows[i].Cells["Companies"].Value);
jourXML1.Body = Convert.ToString(dtgJournal.Rows[i].Cells["Body"].Value);
jourXML1.Duration = Convert.ToInt32(dtgJournal.Rows[i].Cells["Duration"].Value);

JournalXMLList.Add(jourXML1);

folderXML1.JournalListXML = JournalXMLList;
folderListXML.Add(folderXML1);
UserXML.FolderListXML = folderListXML;
UserListXML.Add(UserXML);
ExportXML.UserListXML = UserListXML;
ClsExportCreateXMLForXML XMLCreaterXML = new ClsExportCreateXMLForXML();
XMLCreaterXML.CreateXMLForXML(ExportXML, browsefolder.SelectedPath);
JournalXMLList.Clear();
folderListXML.Clear();
UserListXML.Clear();
}
}

dtgJournal.ClearSelection();
}


// Calendar
if (dtgCalander.Rows.Count > 0)
{
for (int i = 0; i < dtgCalander.Rows.Count; i++)
{
if (dtgCalander.Rows[i].Selected == true)
{
ClsExportCalendarXML caleXML1 = new ClsExportCalendarXML();

caleXML1.StartDate = Convert.ToDateTime(dtgCalander.Rows[i].Cells["StartTimeUTC"].Value);
caleXML1.EndDate = Convert.ToDateTime(dtgCalander.Rows[i].Cells["EndTimeUTC"].Value);
caleXML1.Subject = Convert.ToString(dtgCalander.Rows[i].Cells["Subject"].Value);
caleXML1.Location = Convert.ToString(dtgCalander.Rows[i].Cells["Location"].Value);
caleXML1.Body = Convert.ToString(dtgCalander.Rows[i].Cells["Body"].Value);
caleXML1.RecuranceState = Convert.ToString(dtgCalander.Rows[i].Cells["RecurrenceState"].Value);

CalendarXMLList.Add(caleXML1);

folderXML1.CalendarListXML = CalendarXMLList;
folderListXML.Add(folderXML1);
UserXML.FolderListXML = folderListXML;
UserListXML.Add(UserXML);
ExportXML.UserListXML = UserListXML;
ClsExportCreateXMLForXML XMLCreaterXML = new ClsExportCreateXMLForXML();
XMLCreaterXML.CreateXMLForXML(ExportXML, browsefolder.SelectedPath);
CalendarXMLList.Clear();
folderListXML.Clear();
UserListXML.Clear();
}
}

dtgCalander.ClearSelection();
}

// Note
if (dtgNote.Rows.Count > 0)
{
for (int i = 0; i < dtgNote.Rows.Count; i++)
{
if (dtgNote.Rows[i].Selected == true)
{

ClsExportNotesXML notXML1 = new ClsExportNotesXML();
notXML1.SentDate = Convert.ToDateTime(dtgNote.Rows[i].Cells["CreationTime"].Value);
notXML1.Subject = Convert.ToString(dtgNote.Rows[i].Cells["Subject"].Value);
NoteXMLList.Add(notXML1);

folderXML1.NoteListXML = NoteXMLList;
folderListXML.Add(folderXML1);
UserXML.FolderListXML = folderListXML;
UserListXML.Add(UserXML);
ExportXML.UserListXML = UserListXML;
ClsExportCreateXMLForXML XMLCreaterXML = new ClsExportCreateXMLForXML();
XMLCreaterXML.CreateXMLForXML(ExportXML, browsefolder.SelectedPath);
NoteXMLList.Clear();
folderListXML.Clear();
UserListXML.Clear();
}
}
dtgNote.ClearSelection();

}
if (dtgMail.Rows.Count > 0)
{
for (int i = 0; i < dtgMail.Rows.Count; i++)
{
if (dtgMail.Rows[i].Selected == true)
{
ClsExportMailXML MailXML1 = new ClsExportMailXML();
MailXML1.SentDate = Convert.ToDateTime(dtgMail.Rows[i].Cells["MailDate"].Value);
MailXML1.Subject = Convert.ToString(dtgMail.Rows[i].Cells["MailSubject"].Value);
MailXML1.From = Convert.ToString(dtgMail.Rows[i].Cells["MailFrom"].Value);
MailXML1.To = Convert.ToString(dtgMail.Rows[i].Cells["MailTo"].Value);
MailXML1.Cc = Convert.ToString(dtgMail.Rows[i].Cells["MailCc"].Value);
MailXML1.Bcc = Convert.ToString(dtgMail.Rows[i].Cells["MailBcc"].Value);
MailXML1.MailBodyNew = Convert.ToString(dtgMail.Rows[i].Cells["MailBodyNew"].Value);
MailXMLList.Add(MailXML1);
/////////////////////////
folderXML1.MailListXML = MailXMLList;
folderListXML.Add(folderXML1);
UserXML.FolderListXML = folderListXML;
UserListXML.Add(UserXML);
ExportXML.UserListXML = UserListXML;

ClsExportCreateXMLForXML XMLCreaterXML = new ClsExportCreateXMLForXML();
XMLCreaterXML.CreateXMLForXML(ExportXML, browsefolder.SelectedPath);

MailXMLList.Clear();
folderListXML.Clear();
UserListXML.Clear();



}
}

dtgMail.ClearSelection();
}
MessageBox.Show("Export to XML Completed Successfully", "Restore", MessageBoxButtons.OK, MessageBoxIcon.None);

}
else
browsefolder.Dispose();



}


private void restoreToMSGToolStripMenuItem2_Click(object sender, EventArgs e)
{
if (TreeMailNodes.SelectedNode != null)
{
string NodeName = "";
string strNodes = TreeMailNodes.SelectedNode.FullPath;
String[] Nodes = strNodes.Split('\\');
//Nodes.Remove(0);

for (int i = 1; i < Nodes.Length; i++)
{

NodeName = NodeName + Nodes[i];
if (i != Nodes.Length - 1)
NodeName = NodeName + "\\";

}


string RouteFolderName = MailService.GetTopFolderBySubFolderId(Convert.ToInt64(TreeMailNodes.SelectedNode.Tag));

clsExport Export = new clsExport();
Export.UserCount = 1;
List<ClsExportUserMetaData> userList = new List<ClsExportUserMetaData>();
ClsExportUserMetaData user = new ClsExportUserMetaData();
user.ExportType = ExportTypeEnum.Selected;
user.UserName = RouteFolderName;
List<clsExportFolder> folderList = new List<clsExportFolder>();
clsExportFolder folder1 = new clsExportFolder();
folder1.ExportType = ExportTypeEnum.Selected;
folder1.FolderName = NodeName;
List<clsExportMail> MailList = new List<clsExportMail>();
List<clsExportTask> TaskList = new List<clsExportTask>();
List<ClsExportContact> ContactList = new List<ClsExportContact>();
List<ClsExportJournal> JournalList = new List<ClsExportJournal>();
List<ClsExportCalendar> CalendarList = new List<ClsExportCalendar>();
List<ClsExportNote> NoteList = new List<ClsExportNote>();

// DataGridViewRow[] RowList = dtgMail.SelectedRows;

for (int i = 0; i < dtgMail.Rows.Count; i++)
{
if (dtgMail.Rows[i].Selected == true)
{
clsExportMail Mail1 = new clsExportMail();
Mail1.SentDate = Convert.ToDateTime(dtgMail.Rows[i].Cells["MailDate"].Value);
Mail1.Subject = Convert.ToString(dtgMail.Rows[i].Cells["MailSubject"].Value);
MailList.Add(Mail1);
}
}

for (int i = 0; i < dtgTask.Rows.Count; i++)
{
if (dtgTask.Rows[i].Selected == true)
{
clsExportTask task1 = new clsExportTask();
task1.SentDate = Convert.ToDateTime(dtgTask.Rows[i].Cells["CreationTime"].Value);
task1.Subject = Convert.ToString(dtgTask.Rows[i].Cells["Subject"].Value);
TaskList.Add(task1);
}
}

for (int i = 0; i < dtgContact.Rows.Count; i++)
{
if (dtgContact.Rows[i].Selected == true)
{
ClsExportContact cont1 = new ClsExportContact();
cont1.SentDate = Convert.ToDateTime(dtgContact.Rows[i].Cells["CreationTime"].Value);
//cont1.SentDate = Convert.ToString(dtgContact.Rows[i].Cells["Email"].Value);
cont1.Name = Convert.ToString(dtgContact.Rows[i].Cells["FullName"].Value);
ContactList.Add(cont1);
}
}


for (int i = 0; i < dtgJournal.Rows.Count; i++)
{
if (dtgJournal.Rows[i].Selected == true)
{

ClsExportJournal jour1 = new ClsExportJournal();
jour1.SentDate = Convert.ToDateTime(dtgJournal.Rows[i].Cells["CreationTime"].Value);
jour1.Subject = Convert.ToString(dtgJournal.Rows[i].Cells["Subject"].Value);
JournalList.Add(jour1);
}
}

for (int i = 0; i < dtgCalander.Rows.Count; i++)
{
if (dtgCalander.Rows[i].Selected == true)
{

ClsExportCalendar cale1 = new ClsExportCalendar();
cale1.SentDate = Convert.ToDateTime(dtgCalander.Rows[i].Cells["CreationTime"].Value);
cale1.Subject = Convert.ToString(dtgCalander.Rows[i].Cells["Subject"].Value);
CalendarList.Add(cale1);
}
}


for (int i = 0; i < dtgNote.Rows.Count; i++)
{
if (dtgNote.Rows[i].Selected == true)
{

ClsExportNote not1 = new ClsExportNote();
not1.SentDate = Convert.ToDateTime(dtgNote.Rows[i].Cells["CreationTime"].Value);
not1.Subject = Convert.ToString(dtgNote.Rows[i].Cells["Subject"].Value);
NoteList.Add(not1);
}
}



folder1.MailList = MailList;
folder1.TaskList = TaskList;
folder1.ContactList = ContactList;
folder1.JournalList = JournalList;
folder1.CalendarList = CalendarList;
folder1.NoteList = NoteList;



folderList.Add(folder1);

user.FolderList = folderList;
userList.Add(user);
Export.UserList = userList;
ClsExportCreateXML XMLCreater = new ClsExportCreateXML();
XMLCreater.CreateXML(Export);


//MessageBox.Show("Live Exchange Server");
}
FolderBrowserDialog browsefolder = new FolderBrowserDialog();


if (browsefolder.ShowDialog() == DialogResult.OK)
{
try
{

ExportToMsg.strFullFolderPath = "";
ExportToMsg.strRootFolder = null;
List<ExportToMsg.UserInfo> lsUserInfoList = new List<ExportToMsg.UserInfo>();


ExportToMsg.ReadXMLFile(System.Environment.CurrentDirectory + "\\ExportMail.xml", ref lsUserInfoList);
for (int iUserCount = 0; iUserCount < lsUserInfoList.Count; iUserCount++)
{
string pstFilePath = lsUserInfoList[iUserCount].strUserName;
string pstName = null;
String strSaveFolderPath = browsefolder.SelectedPath;
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
outlookNs.AddStore(pstFilePath);
Microsoft.Office.Interop.Outlook.MAPIFolder rootFolder = null;
Microsoft.Office.Interop.Outlook.Stores Store1 = outlookNs.Stores;
foreach (Microsoft.Office.Interop.Outlook.Store item_Store in Store1)
{
string StoreName = item_Store.DisplayName;

if (StoreName == "Personal Folders")
pstName = "Personal Folders";
if (StoreName == "Source Folder")
pstName = "Source Folder";
if (StoreName == "Destination Folder")
pstName = "Destination Folder";
}
rootFolder = outlookNs.Stores[pstName].GetRootFolder();
Microsoft.Office.Interop.Outlook.Folders subFolders = rootFolder.Folders;

if (lsUserInfoList[iUserCount].strType1 == "Full")
{
foreach (Microsoft.Office.Interop.Outlook.Folder folder in subFolders)
{
ExportToMsg.GetSubFolders(folder, strSaveFolderPath, "", lsUserInfoList[iUserCount], 0);
}
}
else
if (lsUserInfoList[iUserCount].strType1 == "Selected")
{
for (int iFolderCount = 0; iFolderCount < lsUserInfoList[iUserCount].lsItemInfo.Count; iFolderCount++)
{
ExportToMsg.strRootFolder = lsUserInfoList[iUserCount].lsItemInfo[iFolderCount].strFolderName.Split('\\');
if (lsUserInfoList[iUserCount].strType2 == "Full")
{
foreach (Microsoft.Office.Interop.Outlook.Folder folder in subFolders)
{
if (ExportToMsg.strRootFolder[0] == folder.Name)
{
ExportToMsg.strFullFolderPath = folder.Name;
ExportToMsg.GetSubFolders(folder, strSaveFolderPath, lsUserInfoList[iUserCount].lsItemInfo[iFolderCount].strFolderName, lsUserInfoList[iUserCount], iFolderCount);
}
}
}
else if (lsUserInfoList[iUserCount].strType2 == "Selected")
{
foreach (Microsoft.Office.Interop.Outlook.Folder folder in subFolders)
{
if (ExportToMsg.strRootFolder[0] == folder.Name)
{
ExportToMsg.strFullFolderPath = folder.Name;
ExportToMsg.GetSubFolders(folder, strSaveFolderPath, lsUserInfoList[iUserCount].lsItemInfo[iFolderCount].strFolderName, lsUserInfoList[iUserCount], iFolderCount);
}
}
}

}
}
outlookNs.RemoveStore(rootFolder);
Process[] GetAllProcesses = Process.GetProcessesByName("OUTLOOK");
for (int i = 0; i < GetAllProcesses.Length; i++)
{
GetAllProcesses[i].Kill();
}

}



MessageBox.Show("Export to MSG is Completed", "Export", MessageBoxButtons.OK, MessageBoxIcon.None);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
Process[] GetAllProcesses = Process.GetProcessesByName("OUTLOOK");
for (int i = 0; i < GetAllProcesses.Length; i++)
{
GetAllProcesses[i].Kill();
}
}


}
else
{
browsefolder.Dispose();
}
browsefolder.Dispose();
browsefolder = null;
}


private void IterateFolders(TreeNode oTree,ref List<clsExportFolder>folderList)
{

foreach (TreeNode oSubtree in oTree.Nodes)
{

if (oSubtree.Checked == true)
{
clsExportFolder folder1 = new clsExportFolder();
folder1.ExportType = ExportTypeEnum.Full;
string NodeName = "";
string strNodes = oSubtree.FullPath;//TreeMailNodes.SelectedNode.FullPath;
String[] Nodes = strNodes.Split('\\');
//Nodes.Remove(0);


for (int i = 1; i < Nodes.Length; i++)
{

NodeName = NodeName + Nodes[i];
if (i != Nodes.Length - 1)
NodeName = NodeName + "\\";
}
folder1.FolderName = NodeName;
folderList.Add(folder1);
}
else
if (oSubtree.Nodes.Count > 0)
{
IterateFolders(oSubtree, ref folderList);
}

}

}


Thanks And Regards.
Kamlesh Nikam


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend