1

I am developing a windows forms application. I use DataGridView and data binding with LINQ to SQL. It's working. It binds the whole data.

I need some fields instead of whole table data, for example:

The Customers table contains five fields (name, address, mobile, email, age).

I use the code below to get whole data.

private void AssignLocation_Load(object sender, EventArgs e)
{
    // Get Datacontext object to connect with data source
    SmartxBillingSystemDataContext dc = new SmartxBillingSystemDataContext();

    // create table object
    Customer customer = new Customer();
    var allcustomers = from c in dc.GetTable<Customer>() select c;

    // bind to datagrid
    customerBindingSource.DataSource = allcustomers;

    // now assign binding source to data grid view
    dataGridView1.DataSource = customerBindingSource;

}    

Now I just need some fields, e.g. Name and Address.

Additionally, I need to add a button in each row like so:

| A Name       | An Address   | Button |
| Another Name | Fake Address | Button |

How can I achieve this? Please help.

For click event I use below code

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            popUpLocationWindow();
        }

        private void popUpLocationWindow()
        {
            MessageBox.Show("I am clicked");
        }

But this code not works.

Salman Mushtaq
  • 333
  • 3
  • 19
  • Does https://stackoverflow.com/questions/6960739/how-to-hide-column-of-datagridview-when-using-custom-datasource help? – mjwills Aug 17 '17 at 11:50
  • @mjwills, I am new in developing and don't get idea of provided link. Can you please give me some idea related to my scenario so it's easy for me to digest. – Salman Mushtaq Aug 17 '17 at 11:54
  • You have to add one by one data member to your gridviewthose you want **datafieldname** should be same as object property in order to bind also extra field with button type to have extra column. – Abhishek Sharma Aug 17 '17 at 12:14

1 Answers1

2

You can create your own class:

public class MyCustomer
{
    public string Name { get; set; }
    public string Address { get; set; }
}

and select like this:

var allMycustomers = from c in dc.GetTable<Customer>() select new MyCustomer { Name = c.Name, Address = c.Address };

customerBindingSource.DataSource = allMycustomers;

for button:

DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.HeaderText = "Button";
btn.Text = "Click Me!";
btn.Name = "btn";
dataGridView1.Columns.Add(btn);
btn.UseColumnTextForButtonValue = true;

Update for button click event you can use dataGridView1_CellClick event..

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
     if (e.ColumnIndex == 2)
     {
            MessageBox.Show((e.RowIndex+1)  + "  Row  " + (e.ColumnIndex+1) + "  Column button clicked ");
     }
}
caner
  • 681
  • 5
  • 18
  • dear caner, thanks for help. Yes its working. Just one more thing. If I need to make method on button click how I do this? – Salman Mushtaq Aug 17 '17 at 12:26
  • thanks for reply and method. Using your code where I assign this method with button. Means there is no OnClick method. – Salman Mushtaq Aug 17 '17 at 12:47
  • @SalmanMushtaq sorry there is none :( You can handle it with views cell click event... – caner Aug 17 '17 at 13:11
  • @SalmanMushtaq `dataGridView1.CellClick += dataGridView1_CellClick;` is your click event defined? – caner Aug 17 '17 at 13:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152158/discussion-between-caner-and-salman-mushtaq). – caner Aug 17 '17 at 13:43