Q:

How to export a txt file in rows with the same category in a DataGridView?

I need to export all the Grid that is one file per row, but if they have the same category, export each row within a same txt file, if at all Grid there is only one file with category 8 for example, that row will be a txt file.

A:

I am assuming that you want to create different text file for each category group. 
Try this:  
  private void SaveToTextFIle()
    {
        //get all distinct categories
        DataView view = new DataView((DataTable)dataGridView1.DataSource);
        DataTable distinctValues = view.ToTable(true, "CategoryID");

        //save each category
        foreach (DataRow categoryRow in distinctValues.Rows)
        {
            var sb = new StringBuilder();

            //if you want headers
            var headers = dataGridView1.Columns.Cast<DataGridViewColumn>();
            sb.AppendLine(string.Join(",", headers.Select(column => "\"" + column.HeaderText + "\"").ToArray()));

            //form actual data available for this category
            foreach (DataGridViewRow row in dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => r.Cells["CategoryID"].Value.ToString() == categoryRow["CategoryID"].ToString()))
            {
                var cells = row.Cells.Cast<DataGridViewCell>();
                sb.AppendLine(string.Join(",", cells.Select(cell => "\"" + cell.Value + "\"").ToArray()));
            }

            //write to file
            File.WriteAllText(@"D:\" + categoryRow["CategoryID"] + ".txt", sb.ToString());
         }
    }

