Question

I am using the following code to export Selected rows from DatagGridview to Word document. Code is working but there is one Problem. It is exporting the last row first and i can't figure out a way to make it Export in order. For example, If i select Row[0,1,2,3] it will export row [3] than [2] and [1]. Any Ideas what the Problem is?

public void WordDoc(string getfilename)
        {



            object FileName = getfilename;


            //Create word Application Object
            Word.Application word = new Word.Application();

            //Create word document Object
            Word.Document doc = null;

            //Create word Missing Object
            object missing = System.Type.Missing;

            object readOnly = false;
            object isVisible = false;
            // make visible Word application
            word.Visible = true;

            try
            {
                doc = word.Documents.Open(ref FileName, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);
                doc.Activate();    
foreach (DataGridViewRow rows in dataGridView1.SelectedRows)
                        {
                        string item1 = rows.Cells[0].Value.ToString();
                                string item2 = rows.Cells[2].Value.ToString();
                                string item3 = rows.Cells[3].Value.ToString();
                                string item4 = rows.Cells[4].Value.ToString();
                                string item5 = rows.Cells[5].Value.ToString();
                                string item6 = rows.Cells[6].Value.ToString();
                                string item7 = rows.Cells[7].Value.ToString();
                                string item8 = rows.Cells[8].Value.ToString();
                                string item9 = rows.Cells[9].Value.ToString();
                                string item10 = rows.Cells[10].Value.ToString();
                                string item11 = rows.Cells[11].Value.ToString();
                                string item12 = rows.Cells[12].Value.ToString();

                                this.FindAndReplace(word, "!0!", item1);
                                this.FindAndReplace(word, "!1!", item2);
                                this.FindAndReplace(word, "!2!", item3);
                                this.FindAndReplace(word, "!3!", item4);
                                this.FindAndReplace(word, "!4!", item5);
                                this.FindAndReplace(word, "!5!", item6);
                                this.FindAndReplace(word, "!6!", item7);
                                this.FindAndReplace(word, "!7!", item8);
                                this.FindAndReplace(word, "!8!", item9);
                                this.FindAndReplace(word, "!9!", item10);
                                this.FindAndReplace(word, "!10!", item11);
                                this.FindAndReplace(word, "!11!", item12);
                        }
}
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }



            private void FindAndReplace(Word.Application word, object findText, object replaceText)
            {
                word.Selection.Find.ClearFormatting();
                object matchCase = true;
                object matchWholeWord = true;
                object matchWildCards = false;
                object matchSoundsLike = false;
                object matchAllWordForms = false;
                object forward = true;
                object format = true;
                object matchKashida = false;
                object matchDiacritics = false;
                object matchAlefHamza = false;
                object matchControl = false;
                object read_only = false;
                object visible = true;
                object replace = 1;
                object wrap = 2;

                word.Selection.Find.Execute(ref findText, ref matchCase,
                ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
                ref matchAllWordForms, ref forward, ref wrap, ref format,
                ref replaceText, ref replace, ref matchKashida,
                ref matchDiacritics,
                ref matchAlefHamza, ref matchControl);
        }       
Was it helpful?

Solution

I think this Linq should work (you'll need using System.Linq; at the top of the ".cs" file.):

var orderedRows = from DataGridViewRow row in dataGridView1.SelectedRows
                  orderby row.Index
                  select row;

foreach (DataGridViewRow row in orderedRows)
    ...

[EDIT] A little more information as requested by OP:

This is using Linq to sort the returned rows in the order of each row's index.

See this documentation for an introduction to Linq

But briefly, how it works is like this:

Firstly, realise that DataGridView.SelectedRows returns the rows in the order that they were selected by the user.

To get them in the correct order, we need to sort them by each row's .Index property.

The Linq above does this by saying:

"Take all the rows in dataGridView1.SelectedRows and order them by each row's Index property, then return all the rows in that order".

But it's way too complicated to go into fully here; you'll have to read the introduction to Linq that I posted!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top