Uniek Le

Uniek Le

  • NA
  • 2
  • 5.4k

Datagridview Combobox make members from column values

Oct 9 2012 6:13 AM
Hello everyone,I have some questions about my program coz it has some errors and i can't figure out.


1. I have Datagridview1 and Datagridview2 and i load data for my table1 and works fine with LoadDataRow(vals.ToArray, false); but when i use it fro my table 2 it doesn't work. It throws me with ArgumentException


2.In my Datagridview2 I made Combobox, works great but now I want to make this members from column values, is that possible? 


 
DataTable
table = new DataTable("table");
table.Columns.Add("ID", typeof(Int32));
table.Columns.Add("Name", typeof(String)); 
table.Columns.Add("Produkt", typeof(String));
table.Columns.Add("Attribute", typeof(String));
 
DataTable table2 = new DataTable("table2");
 table2.Columns.Add("Name", typeof(String));

table2.Columns.Add("Comment", typeof(String));
 
DataSet ds = new DataSet("DataSet");

ds.Tables.Add(table);
 ds.Tables.Add(table2);
 
object[] o1 = { 1, "Mike", "AA", "aa" };
object[] o2 = { 2,"Steve","BB","aa" };
object[] o3 = { 3,"None",null , "cc" };
object[] o4 = { 4,"Hube", "DD", };
object[] c1 = { "None","XX" };
object[] c2 = { "Steve","YY"};
object[] c3 = { "Mike", "YY"};
 table.Rows.Add(o1);
 table.Rows.Add(o2);
 table.Rows.Add(o3);
 table.Rows.Add(o4);
 table2.Rows.Add(c1);
 table2.Rows.Add(c2);
 table2.Rows.Add(c3);
 
var results = from t1 in table.AsEnumerable()
join tb2 in table2.AsEnumerable()
on t1.Field<string>("Name") equals tb2.Field<string>("Name") into prodGroup
from table4 in prodGroup.DefaultIfEmpty()
select new

{

ID = t1.Field<Int32?>("ID"),
 Name = t1.Field<String>("Name"),
Produkt = t1.Field<String>("Produkt"),
 Attribute = t1.Field<String>("Attribute"),

Comment = table4 != null ? table4.Field<String>("Comment") : null,
 } ;
 
var result = new DataTable();

result.Columns.Add("ID", typeof(Int32));

result.Columns.Add("Name", typeof(String));

result.Columns.Add("Produkt", typeof(String));

result.Columns.Add("Comment", typeof(String));

result.Columns.Add("Attribute", typeof(String));
 
foreach (var r in results)
 {
var vals = new List<object>() { r.ID, r.Name, r.Produkt, r.Comment, r.Attribute };
 
result.BeginLoadData();

result.LoadDataRow(vals.ToArray(), false);

}

dataGridView2.AutoGenerateColumns = false;


var AttValueType = new DataGridViewComboBoxColumn();

AttValueType.DataSource =new List<string>() { "aa", "bb", "cc" };

AttValueType.HeaderText = "Attribute";

AttValueType.DataPropertyName = "Attribute";
DataGridViewTextBoxColumn ID = new DataGridViewTextBoxColumn();

ID.HeaderText = "ID";

ID.DataPropertyName = "ID";
DataGridViewTextBoxColumn Name = new DataGridViewTextBoxColumn();

Name.HeaderText = "Name";

Name.DataPropertyName = "Name";


DataGridViewTextBoxColumn Produkt = new DataGridViewTextBoxColumn();

Produkt.HeaderText = "Produkt";

Produkt.DataPropertyName = "Produkt";


DataGridViewTextBoxColumn Comment = new DataGridViewTextBoxColumn();

Comment.HeaderText = "Comment";

Comment.DataPropertyName = "Comment";

dataGridView2.DataSource = result;

dataGridView2.Columns.AddRange(ID, Name, Produkt, AttValueType, Comment);

dataGridView2.BeginEdit(true);
 }
DataTable
table = new DataTable("table");
table.Columns.Add("ID", typeof(Int32));
table.Columns.Add("Name", typeof(String));
table.Columns.Add("Produkt", typeof(String));
table.Columns.Add("Attribute", typeof(String));

DataTable table2 = new DataTable("table2");
table2.Columns.Add("Name", typeof(String));
table2.Columns.Add("Comment", typeof(String));

DataSet ds = new DataSet("DataSet");
ds.Tables.Add(table);
ds.Tables.Add(table2);

object[] o1 = { 1, "Mike", "AA", "aa" };

object[] o2 = { 2,"Steve","BB","aa" };

object[] o3 = { 3,"None",null , "cc" };

object[] o4 = { 4,"Hube", "DD", };

object[] c1 = { "None","XX" };

object[] c2 = { "Steve","YY"};

object[] c3 = { "Mike", "YY"};
table.Rows.Add(o1);
table.Rows.Add(o2);
table.Rows.Add(o3);
table.Rows.Add(o4);
table2.Rows.Add(c1);
table2.Rows.Add(c2);
table2.Rows.Add(c3);


var results = from t1 in table.AsEnumerable()

join tb2 in table2.AsEnumerable()

on t1.Field<string>("Name") equals tb2.Field<string>("Name") into prodGroup

from table4 in prodGroup.DefaultIfEmpty()

select new
{
ID = t1.Field<Int32?>("ID"),
Name = t1.Field<String>("Name"),
Produkt = t1.Field<String>("Produkt"),
Attribute = t1.Field<String>("Attribute"),
Comment = table4 != null ? table4.Field<String>("Comment") : null,
} ;
 
var result = new DataTable();
result.Columns.Add("ID", typeof(Int32));
result.Columns.Add("Name", typeof(String));
result.Columns.Add("Produkt", typeof(String));
result.Columns.Add("Comment", typeof(String));
result.Columns.Add("Attribute", typeof(String));

foreach (var r in results)
{

var vals = new List<object>() { r.ID, r.Name, r.Produkt, r.Comment, r.Attribute };

result.BeginLoadData();
result.LoadDataRow(vals.ToArray(), false);
}
dataGridView2.AutoGenerateColumns =false;

var AttValueType = new DataGridViewComboBoxColumn();
AttValueType.DataSource =
new List<string>() { "aa", "bb", "cc" };
AttValueType.HeaderText = "Attribute";
AttValueType.DataPropertyName = "Attribute";

DataGridViewTextBoxColumn ID = new DataGridViewTextBoxColumn();
ID.HeaderText = "ID";
ID.DataPropertyName = "ID";

DataGridViewTextBoxColumn Name = new DataGridViewTextBoxColumn();
Name.HeaderText = "Name";
Name.DataPropertyName = "Name";

DataGridViewTextBoxColumn Produkt = new DataGridViewTextBoxColumn();
Produkt.HeaderText = "Produkt";
Produkt.DataPropertyName = "Produkt";

DataGridViewTextBoxColumn Comment = new DataGridViewTextBoxColumn();
Comment.HeaderText = "Comment";
Comment.DataPropertyName = "Comment";
dataGridView2.DataSource = result;
dataGridView2.Columns.AddRange(ID, Name, Produkt, AttValueType, Comment);
dataGridView2.BeginEdit(true);
}