Click here to Skip to main content
15,896,522 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to open a folder using datagridviewbutton "IMAGE" and the folder name is rowindex[0], here it is "MR_NO".
there are six column with names "MR_NO,NAME,GENDER,DOB,PATH,IMAGE".
Here "IMAGE" column is DataGridViewButton. I am not getting any value or messages here,I don't know where I am wrong.

"Here problem is that I am unable to use DatagridViewbutton 'IMAGE' and use MR_NO column value in the button to open a folder"
Remaining things are correct.

What I have tried:

I used the following code.
CODE:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient; 
using System.Windows.Forms;

namespace Autoupload
{
    public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection("Integrated Security=true;Initial Catalog=hospital;server=localhost\\sqlexpress");
        public Form1()
        {
            InitializeComponent();
            load();
        }
        void load()
        {
            cn.Open();
            SqlCommand cmddatabase = new SqlCommand("GetCustomers", cn);
            try
            {
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmddatabase;
                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bsource = new BindingSource();
                bsource.DataSource = dbdataset;
                dataGridView1.DataSource = bsource;
                sda.Update(dbdataset);
          /*  DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(btn);
            btn.HeaderText = "IMAGES";
            btn.Text = "Click Here";
            btn.Name = "btn";
            btn.UseColumnTextForButtonValue = true;*/


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
      /*  private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dataGridView1.Columns[e.ColumnIndex].Name == "IMAGE")
            {
                string mrnum=dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
               System.Diagnostics.Process.Start("D:\\ "+mrnum);
            }
        
        }*/

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'hospitalDataSet.GetCustomers' table. You can move, or remove it, as needed.
            this.getCustomersTableAdapter.Fill(this.hospitalDataSet.GetCustomers);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "IMAGE")
            {
                  string mrnum=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                  System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
            {
                FileName ="D:\\ "+mrnum,
                UseShellExecute = true,
                Verb = "open"
            });
            }
        }
    }
Posted
Updated 10-Apr-16 0:07am
v6
Comments
Sergey Alexandrovich Kryukov 10-Apr-16 3:39am    
There is no such concept, "open a folder".
—SA

We can't tell you what to do - we don't have access to your data!
So start with the debugger, and put a breakpoint on the line:
C#
System.Diagnostics.Process.Start("D:\\ "+mrnum);
Run your app, and when it reaches the line, it will stop. Visual Studio will then wait for you to tell it what to do.
Use the debugger to look at the value in mrnum and mentally add the "D:\" part to form a full string. Look at your file system, and see if the folder you get as a result actually exists. The string has to be a full, proper, valid file specification or it will just fail. It's worth doing this via:
C#
string path = Path.Combine("D:\\ ", mrnum);
System.Diagnostics.Process.Start(path);
so you can see exactly what is going on.
But... I suspect it's that space in the string, myself...
[edit]I hate markup![/edit]
 
Share this answer
 
v3
Comments
aurj 10-Apr-16 5:14am    
my question is not related to opening the folder else regarding datagridviewbutton
You could use this if you want to open the folder and select a file:
C#
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\MR_NO" ;

See: OpenFileDialog Class (System.Windows.Forms)[^]

Use this if you only want to open a folder:
C#
private FolderBrowserDialog folderBrowserDialog1;
private OpenFileDialog openFileDialog1;

See: FolderBrowserDialog Class (System.Windows.Forms)[^]

Or use this if you want Explorer to open:
C#
System.Diagnostics.Process.Start("Explorer.exe", @"/select,""" + FilePath + "\"")
 
Share this answer
 
v2
Comments
aurj 10-Apr-16 5:14am    
my question is not related to opening the folder else regarding datagridviewbutton
RickZeeland 10-Apr-16 5:16am    
yes it is !
I quote "I want to open a folder using datagridviewbutton"
Aha, I think I now understand your problem, try this:
C#
// Add CellContentClick event to your DataGridView first in Designer
// and remove other possible conflicting event handlers.
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "IMAGE")
    {
        //  your code...
        var str = myDataGridView.CurrentRow.Cells[0].Value.ToString();
        MessageBox.Show("You Have Selected " + str);
    }
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900