Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
40
UltraGrid Code Please!
posted

Hi,

 

I need your help to implement the below code using ultragrid in C# windows application. THe below code is the code that is working in Microsoft DataGridView. This button 1: code basically uses a file open dialog to select multiple files and display in a DataGridView. button 2: code will copy the selected files from the DataGridView into C:\Temp folder.

 I  need to implement the same functionality using UltraGrid. I am very new to these controls. Please help me.

 

Thanks.

 

 

         private void button1_Click(object sender, EventArgs e)
        {
            //string x = "";
            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "All files (*.*)|*.*";
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                int number_display = 1;
                int row_no = 0;

                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    for (int j = 0; j < openFileDialog1.FileNames.Length ; j++)
                    {
                        if (j >= dataGridView1.RowCount)
                        {
                            dataGridView1.Rows.Add();
                        }
                    }

                    foreach (String file in openFileDialog1.FileNames)
                    {
                        string filePath = file;
                        int extbeginPosition = filePath.LastIndexOf("\\");
                        string fName = filePath.Substring(extbeginPosition + 1);
                        string fLocation = filePath.Substring(0, extbeginPosition);

                        if (row_no < dataGridView1.RowCount)
                        {
                            dataGridView1.Rows[row_no].Selected = true;

                            dataGridView1.Rows[row_no].Cells[0].Value = number_display;
                            dataGridView1.Rows[row_no].Cells[1].Value = fName;
                            dataGridView1.Rows[row_no].Cells[2].Value = fLocation;
                        }
                       
                        row_no++;
                        number_display++;

                    }

                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string fName = "";
            string fLocation = "";
            string fullName = "";
            string copyFlag = "False";

            string destinationLocation = "C:\\Temp\\";

            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                fName = dataGridView1.Rows[i].Cells[1].Value.ToString();
                fLocation = dataGridView1.Rows[i].Cells[2].Value.ToString();
                fullName = fLocation + "\\" + fName;
                if (dataGridView1.Rows[i].Cells[4].Value != null)
                {
                    copyFlag = dataGridView1.Rows[i].Cells[4].Value.ToString();
                }

                if (copyFlag == "True")
                {
                    // Create the file and clean up handles.
                    using (FileStream fs = File.Create(fullName)) { }

                    // Ensure that the target does not exist.
                    File.Delete(destinationLocation + fName);

                    // Copy the file.
                    File.Copy(fullName, destinationLocation + fName, true);

                    copyFlag = "False";
                }


            }

        }

  • 469350
    Verified Answer
    Offline posted

    UltraGrid cannot be used without a DataSource of some kind. So you will need to load the data from your files intoan object that supports IList or IBindingList.

    There are many objects you can use for this, depending on the needs of your application. You could create a DataTable and populate it with the data. You could use an UltraDataSource. Or you could even use a BindingList<T> and populate it with custom objects you create.