How add button in DataGridView - c#

How can I add button to my DataGridView code, in two cell (c#)
table have 4 columns, and dinamically rows;
last two columns had button.
Code is:
DataTable table = new DataTable();
DataColumn col1 = new DataColumn("Típus (Név)", typeof(string));
DataColumn col2 = new DataColumn("Ertéke", typeof(string));
DataColumn col3 = new DataColumn("Szerkesztés",typeof(Button));
DataColumn col4 = new DataColumn("Törlés",typeof(Button));
table.Columns.Add(col1);
table.Columns.Add(col2);
table.Columns.Add(col3);
table.Columns.Add(col4);
foreach (var item in data)
{
string[] listdata = item.Split('|');
DataRow row = table.NewRow();
string nev = listdata[0];
string ertek = listdata[1];
DataGridViewButtonCell torol = new DataGridViewButtonCell();
torol.Tag = listdata[2];
Button szerkeszt = new Button();
szerkeszt.Text = "Edit";
Button Torol = new Button();
Torol.Text = "Delete";
row[col1] = nev;
row[col2] = ertek;
row[col3] = szerkeszt;
row[col4] = Torol;
table.Rows.Add(row);
}
DataGridView gvTest = new DataGridView();
gvTest.DataSource = table;
gvTest.Size = new Size(700,400);
gvTest.Font = new Font(gvTest.Font.Name,13);
gvTest.ReadOnly = true;
panel.Controls.Add(gvTest);

Try like this
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
gvTest.Columns.Add(btn);
btn.HeaderText = "Click Data";
btn.Text = "Click Here";
btn.Name = "btn";
More Reference here

Related

Why doesn't it show me the values in my DataGrid that I assign in the DataTable?

I initially wrote the headers manually in my DataGrid and now I want to fill the DataGrid from a DataTable. I wanted to do it like this:
void fillingDataGrid()
{
DataTable dt = new DataTable();
DataColumn id = new DataColumn("id", typeof(int));
DataColumn name = new DataColumn("name", typeof(string));
DataColumn ort = new DataColumn("ort", typeof(string));
DataColumn alter = new DataColumn("alter", typeof(string));
DataColumn land = new DataColumn("land", typeof(string));
dt.Columns.Add(id);
dt.Columns.Add(name);
dt.Columns.Add(ort);
dt.Columns.Add(alter);
dt.Columns.Add(land);
DataRow firstrow = dt.NewRow();
firstrow[0] = 1;
firstrow[1] = "Peter";
firstrow[2] = "Berlin";
firstrow[3] = "18";
firstrow[4] = "Germany";
DataRow secondrow = dt.NewRow();
firstrow[0] = 2;
firstrow[1] = "Karl";
firstrow[2] = "Prag";
firstrow[3] = "12";
firstrow[4] = "Tschechien";
dt.Rows.Add(firstrow);
dt.Rows.Add(secondrow);
gridd.ItemsSource = dt.DefaultView;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.fillingDataGrid();
}
The problem, however, is that if I do it this way, it is not output correctly because it looks like this:
Why doesn't it show me all the data, what do I have to change on my DataTable?
Change this line :
gridd.ItemsSource = dt.DefaultView;
To :
gridd.DataContext = dt.DefaultView;

c# backgroundworker creating datagridview and creating columns

I am getting this error "No row can be added to a DataGridView control that does not have columns. Columns must be added first." what am i doing wrong here?
//create datagridview1
DataGridView dataGridView1 = new DataGridView();
// DataGridViewColumn column = new DataGridViewTextBoxColumn();
// Initialize the DataGridView.
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSize = true;
DataGridViewColumn column1 = new DataGridViewTextBoxColumn();
column1.DataPropertyName = "Column1";
column1.Name = "title";
dataGridView1.Columns.Add(column1);
DataGridViewColumn column2 = new DataGridViewTextBoxColumn();
column2.DataPropertyName = "Column2";
column2.Name = "imageurl";
dataGridView1.Columns.Add(column2);
DataGridViewColumn column3 = new DataGridViewTextBoxColumn();
column3.DataPropertyName = "Column3";
column3.Name = "videourl";
dataGridView1.Columns.Add(column3);
DataGridViewColumn column4 = new DataGridViewTextBoxColumn();
column4.DataPropertyName = "Column4";
column4.Name = "done";
dataGridView1.Columns.Add(column4);
try
dataGridView1.Columns.Add("Column","Test");
or if you want to choose coulmn type
DataGridViewColumn newCol = new DataGridViewColumn(); // add a column to the grid
DataGridViewCell cell = new DataGridViewCell(); //Specify which type of cell in this column
newCol.CellTemplate = cell;
newCol.HeaderText = "test2";
newCol.Name = "test2";
newCol.Visible = true;
newCol.Width = 40;
gridColors.Columns.Add(newCol);
from here
You also can see MSDN example

Set Focus on DataGridViewCombobox while pressing arrow key up and down

I have a datagridview in which there are three columns .actually this is a dataprocessing application thats why i want use of keyboard(minimal use of mouse)
the code is as follows
[code languge="csharp"]
private void Form2_Load(object sender, EventArgs e)
{
DataTable odt = new DataTable();
DataColumn odc = new DataColumn();
DataColumn odcSec = new DataColumn();
DataColumn odcThird = new DataColumn();
DataColumn odcForth = new DataColumn();
odc.ColumnName = "Class";
odt.Columns.Add(odc);
odcSec.ColumnName = "Subject Name";
odt.Columns.Add(odcSec);
odcThird.ColumnName = "Grade";
odt.Columns.Add(odcThird);
odcForth.ColumnName = "GradeCollection";
odt.Columns.Add(odcForth);
DataRow odr = odt.NewRow();
odr["Class"] = "FYBA";
odr["Subject Name"] = "Hindi";
odr["Grade"] = "A";
odr["GradeCollection"] = "A,B,C,D,E";
odt.Rows.Add(odr);
DataRow odrFirst = odt.NewRow();
odrFirst["Class"] = "SYBA";
odrFirst["Subject Name"] = "English";
odrFirst["Grade"] = "B";
odrFirst["GradeCollection"] = "A,B,C,D,E";
odt.Rows.Add(odrFirst);
DataRow odrSecond = odt.NewRow();
odrSecond["Class"] = "SYBA";
odrSecond["Subject Name"] = "English";
odrSecond["Grade"] = "C";
odrSecond["GradeCollection"] = "A,B,C,D,E";
odt.Rows.Add(odrSecond);
DataRow odrThird = odt.NewRow();
odrThird["Class"] = "TYBA";
odrThird["Subject Name"] = "Marathi";
odrThird["Grade"] = "D";
odrThird["GradeCollection"] = "A,B,C,D,E";
odt.Rows.Add(odrThird);
DataRow odrForth = odt.NewRow();
odrForth["Class"] = "FYBA";
odrForth["Subject Name"] = "Telagu";
odrForth["Grade"] = "E";
odrForth["GradeCollection"] = "A,B,C,D,E";
odt.Rows.Add(odrForth);
if (odt != null && odt.Rows.Count > 0)
{
DataGridViewTextBoxColumn txtClass = new DataGridViewTextBoxColumn();
txtClass.HeaderText = "Class";
txtClass.MaxInputLength = 20;
txtClass.Width = 70;
txtClass.Name = "Class";
kryptonDataGridView1.Columns.Add(txtClass);
DataGridViewTextBoxColumn txtSubjectName = new DataGridViewTextBoxColumn();
txtSubjectName.HeaderText = "SubjectName";
txtSubjectName.MaxInputLength = 20;
txtSubjectName.Width = 70;
txtSubjectName.Name = "SubjectName";
kryptonDataGridView1.Columns.Add(txtSubjectName);
DataGridViewComboBoxColumn comboboxColumn = new DataGridViewComboBoxColumn();
comboboxColumn.HeaderText = "Grade";
comboboxColumn.DropDownWidth = 160;
comboboxColumn.Width = 90;
comboboxColumn.MaxDropDownItems = 3;
comboboxColumn.FlatStyle = FlatStyle.Flat;
kryptonDataGridView1.TabStop = true;
kryptonDataGridView1.Focus();
kryptonDataGridView1.Columns.Insert(2, comboboxColumn);
for (int i = 0; i < odt.Rows.Count; ++i)
{
string[] row1 = new string[] { odt.Rows[i]["Class"].ToString(), odt.Rows[i]["Subject Name"].ToString() };
kryptonDataGridView1.Rows.Add(row1);
string sItemNames = odt.Rows[i]["GradeCollection"].ToString();
char[] charArray = new char[] { ',' };
string[] sItemNameArray = sItemNames.Split(charArray, StringSplitOptions.RemoveEmptyEntries);
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("Name");
dt.Columns.Add(dc1);
DataRow odrinner = dt.NewRow();
odrinner["Name"] = "--Select--";
dt.Rows.Add(odrinner);
foreach (string item in sItemNameArray)
{
dt.Rows.Add(item);
}
comboboxColumn.DataSource = dt;
comboboxColumn.DisplayMember = "Name";
if (!string.IsNullOrEmpty(odt.Rows[i]["Grade"].ToString()))
{
kryptonDataGridView1.Rows[i].Cells[2].Value = odt.Rows[i]["Grade"].ToString();
}
else
{
comboboxColumn.DefaultCellStyle.NullValue = "--Select--";
}
}
}
}
[/code]
Now i want to use arrow key as well as enter key for combobox selection but when i press arrow keys then the focus goes to next row combobox column ,it is not selected that particular combo box.In the same when i use enter key then the focus goes to next column.I want when i hit enter key or arrow key then it select combobox(means focus shuld be on combobox) so that i can select items from that combo box without space hit.
You can use this code:
private void kryptonDataGridView1_CellEnter(object sender,
DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
kryptonDataGridView1.BeginEdit(true);
}
}

Cannot see Decimal Value of a Cell in DataGridview C#

I cannot Display the value of "Price" cell on my gridview, here's my code:
DataColumn idCol2 = new DataColumn();
idCol2.DataType = System.Type.GetType("System.Int32");
idCol2.ColumnName = "Id";
table.Columns.Add(idCol2);
DataColumn SKUCol2 = new DataColumn();
SKUCol2.DataType = System.Type.GetType("System.String");
SKUCol2.ColumnName = "SKU";
table.Columns.Add(SKUCol2);
DataColumn ProdNameCol2 = new DataColumn();
ProdNameCol2.DataType = System.Type.GetType("System.String");
ProdNameCol2.ColumnName = "Product Name";
table.Columns.Add(ProdNameCol2);
DataColumn DescCol2 = new DataColumn();
DescCol2.DataType = System.Type.GetType("System.String");
DescCol2.ColumnName = "Product Description";
table.Columns.Add(DescCol2);
DataColumn PriceCol2 = new DataColumn();
PriceCol2.DataType = System.Type.GetType("System.Decimal");
PriceCol2.ColumnName = "Price";
table.Columns.Add(PriceCol2);
DataColumn[] keys2 = new DataColumn[5];
keys2[0] = idCol2;
table.PrimaryKey = keys2;
LivePOS livepos = new LivePOS(clientId, clientSecret);
_products = livepos.GetProducts(apiKey, token);
foreach (var p in _products)
{
table.Rows.Add(p.Id, p.Sku, p.Name, p.Description , p.SellingPrice);
}
table.DefaultView.Sort = "Id ASC";
dataGridView1.DataSource = table;
this.dataGridView1.DefaultCellStyle.Format = "N4";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[0].ReadOnly = false; //id
row.Cells[1].ReadOnly = true; //sku
row.Cells[2].ReadOnly = true; //name
row.Cells[3].ReadOnly = true; //description
}
Please help me :)Is there something wrong in this code? I created new instance of the datatable named "table" on my Form_Load event. All cell values are displaying correctly aside from this "Price" column.
Your Price Column should have Datatype of Double than Decimal.Take a look at this link
Decimal Vs Double
hope that helps

Data Table Not Binding to Grid View

I am creating a data table and trying to bind it wit a grid view.
Code:
DataTable timeTable = new DataTable();
DataColumn colSubject = new DataColumn("Subject Name");
DataColumn colClass = new DataColumn("Class");
DataColumn colTeacher = new DataColumn("Lecturer");
DataColumn colStartTime = new DataColumn("Start");
DataColumn colEndTime = new DataColumn("End");
colSubject.DataType = System.Type.GetType("System.String");
colClass.DataType = System.Type.GetType("System.String");
colTeacher.DataType = System.Type.GetType("System.String");
colStartTime.DataType = System.Type.GetType("System.String");
colEndTime.DataType = System.Type.GetType("System.String");
timeTable.Columns.Add(colSubject);
timeTable.Columns.Add(colClass);
timeTable.Columns.Add(colTeacher);
timeTable.Columns.Add(colStartTime);
timeTable.Columns.Add(colEndTime);
int numOfRows = 0;
SqlDataReader read = null;
try
{
conn.Open();
read = getTimetable.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
if (numOfRows < 3)
{
DataRow row = timeTable.NewRow();
row[colSubject] = read["subject_name"].ToString();
row[colClass] = read["resource_name_number"].ToString();
row[colTeacher] = read["userDetail_name"].ToString();
row[colStartTime] = read["timeSlot_startTime"].ToString();
row[colEndTime] = read["timeSlot_endTime"].ToString();
timeTable.Rows.Add(row);
numOfRows++;
}
}
gridTimeTable.DataSource = timeTable;
gridTimeTable.DataBind();
}
}
Now The Data table is Created Fine and all cause I used a breakpoint to see whether it was being created the thing is it is not populating the grid view. any ideas?
Thanks :)

Categories

Resources