Plz tell me how to add label in datagrid for windows form...
plz help..!
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VasanthPosted Mar 27, 2010, 5:10 AM
private DataTable dtTemp = new DataTable();
private Label lbl = new Label();
DataGridTableStyle dgStyle = new DataGridTableStyle();
DataGridTextBoxColumn dgLabel = new DataGridTextBoxColumn();
private void Form1_Load(object sender, System.EventArgs e)
{
//label property
lbl.ForeColor = Color.Black;
lbl.Font = new Font("TimesRoman", 12, FontStyle.Bold);
dgStyle.GridColumnStyles.Add(dgLabel);
dgLabel.HeaderText = "Name";
dgLabel.MappingName = "Name";
dgLabel.Width = 200;
dgStyle.PreferredRowHeight = 24;
dg .TableStyles.Add(dgStyle);
//Create temp datatable for bind values
DataTable dtTemp = new DataTable(); DataRow dr;
//create schema
dtTemp.Columns.Add("Name"); dtTemp.Columns.Add("City"); dtTemp.Columns.Add("State");
//Add temp datas
dr = dtTemp.NewRow(); dr["Name"] = "AAA"; dr["City"] = "CAAA"; dr["State"] = "SAAA"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Name"] = "BBB"; dr["City"] = "CBBB"; dr["State"] = "SBBB"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Name"] = "CCC"; dr["City"] = "CCCC"; dr["State"] = "SCCC"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Name"] = "DDD"; dr["City"] = "CDDD"; dr["State"] = "SDDD"; dtTemp.Rows.Add(dr);
dr = dtTemp.NewRow(); dr["Name"] = "EEE"; dr["City"] = "CEEE"; dr["State"] = "SEEE"; dtTemp.Rows.Add(dr);
//Bind the datasourse
dg .DataSource = dtTemp;
}
private void dg_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(dg.HitTest(e.X, e.Y) != null)
{
if(dg.HitTest(e.X, e.Y).Column == 0)
{
dgLabel.TextBox.Controls.Add(lbl);
lbl.Text = "Hi Label " + dg[dg.CurrentRowIndex, 0].ToString();
}
}
}
Hope its help , please mark as answer.
Shailendra VermaPosted Mar 31, 2010, 8:40 AM
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataGrid
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataTable dtTemp = new DataTable();
DataRow dr;
private void Form1_Load(object sender, EventArgs e)
{
// creating the object of Datagrid table style
DataGridTableStyle dgStyle = new DataGridTableStyle();
// Creating the object of DataGridTextBoxColumn For Name
DataGridTextBoxColumn dgLabel = new DataGridTextBoxColumn();
// Creating the object of DataGridTextBoxColumn For State
DataGridTextBoxColumn dgLabel1 = new DataGridTextBoxColumn();
// Creating the object of DataGridTextBoxColumn for City
DataGridTextBoxColumn dgLabel2 = new DataGridTextBoxColumn();
// Creating the object of DataGridTextBoxColumn For DateTime
DataGridTextBoxColumn dgDateTime = new DataGridTextBoxColumn();
//Assigning the Header Text Name as "Name" And Mapping Name(Mapping Name is a Name which is in the table's column Name)
dgLabel.HeaderText = "Name";
dgLabel.MappingName = "Name";
dgLabel.Width = 200;
//Assigning the Header Text Name as "State" And Mapping Name(Mapping Name is a Name which is in the table's column Name)
dgLabel1.HeaderText = "State";
dgLabel1.MappingName = "State";
dgLabel1.Width = 200;
//Assigning the Header Text Name as "City" And Mapping Name(Mapping Name is a Name which is in the table's column Name)
dgLabel2.HeaderText = "City";
dgLabel2.MappingName = "City";
dgLabel2.Width = 200;
//Assigning the Header Text Name as "DateTime" And Mapping Name(Mapping Name is a Name which is in the table's column Name)
dgDateTime.HeaderText = "DateTime";
dgDateTime.MappingName = "DateTime";
dgDateTime.Width=200;
// Creating Schema For DataTable
//Adding the column name with name as "Name" in DataTable
dtTemp.Columns.Add("Name");
//Adding the column name with name as "State" in DataTable
dtTemp.Columns.Add("State");
//Adding the column name with name as "City" in DataTable
dtTemp.Columns.Add("City");
//Adding the column name with name as "DateTime" in DataTable
dtTemp.Columns.Add("DateTime");
//Create the rows in DataTable
dr = dtTemp.NewRow();
dr["Name"] = "Shailendra";
dr["State"] = "U.P";
dr["City"] = "Sultanpur";
dr["DateTime"] = "25/08/1988";
dtTemp.Rows.Add(dr);
// Binding The DataTable in DataGrid
dataGrid1.DataSource = dtTemp;
//Creating the object of Label and Setting the properties
Label lbl = new Label();
lbl.Font = new Font("Verdana", 8, FontStyle.Bold);
lbl.Text = "*";
lbl.Left = 150;
lbl.ForeColor = Color.Red;
lbl.BackColor = Color.Green;
//adding the Label control to dgLabel's TextBox
dgLabel.TextBox.Controls.Add(lbl);
//Creating the object of Label1 and Setting the properties
Label lbl1 = new Label();
lbl1.Font = new Font("Verdana", 8, FontStyle.Bold);
lbl1.Left = 140;
lbl1.Text = "*";
lbl1.ForeColor = Color.Red;
lbl1.BackColor = Color.Green;
//adding the Label control to dgLabel1's TextBox
dgLabel1.TextBox.Controls.Add(lbl1);
//Creating the object of Label2 and Setting the properties
Label lbl2 = new Label();
lbl2.Font = new Font("Verdana", 8, FontStyle.Bold);
lbl2.Left = 140;
lbl2.Text = "*";
lbl2.ForeColor = Color.Red;
lbl2.BackColor = Color.Green;
//adding the Label control to dgLabel's TextBox
dgLabel2.TextBox.Controls.Add(lbl2);
//Creating the object of DateTime and Setting the properties
DateTimePicker dtp = new DateTimePicker();
dtp.CalendarForeColor = Color.Gold;
//adding the DateTimePiker control to dgDateTime's TextBox
dgDateTime.TextBox.Controls.Add(dtp);
dgStyle.GridColumnStyles.Add(dgLabel);
dgStyle.GridColumnStyles.Add(dgLabel1);
dgStyle.GridColumnStyles.Add(dgLabel2);
dgStyle.GridColumnStyles.Add(dgDateTime);
dataGrid1.TableStyles.Add(dgStyle);
dgStyle.PreferredRowHeight = 25;
}
}
}
Anusha SrivastavaPosted Mar 27, 2010, 7:05 AM
am not getting this- dataGridLable
Anusha SrivastavaPosted Mar 27, 2010, 7:01 AM
I am trying to make datagrid which have some readable feilds n some editable.
Now i want to show user that the editable feilds are mandatory feilds so i want to use lable in each row Indicating "*" for each editable column.
see i hv done this
DataTable Item = new DataTable();
Item.Columns.Add("ItemCode", typeof(string));
Item.Columns["ItemCode"].ReadOnly = true;
Item.Columns.Add("ItemName", typeof(string));
Item.Columns["ItemName"].ReadOnly = true;
Item.Columns.Add("RecivedQuantity", typeof(Int32));
Item.Columns["RecivedQuantity"].ReadOnly = true;
Item.Columns.Add("Approved", typeof(Int32));
Item.Columns.Add("Rejected", typeof(Int32));
Item.Rows.Add("I009", "RAM", 100, 95, 5);
Item.Rows.Add("I002", "Bolts", 1025, 1000, 25);
Item.Rows.Add("I004", "MotherBoard", 80, 78, 2);
Item.Rows.Add("I005", "ICs", 125, 100, 25);
//binding datagrid and assigning properties to it.
dataGrid1.HeaderForeColor = Color.White;
dataGrid1.HeaderBackColor = Color.Chocolate;
dataGrid1.HeaderFont = new Font("verdana", 8, FontStyle.Bold);
dataGrid1.CaptionText = "Check Item";
dataGrid1.Width = 415;
dataGrid1.DataSource = Item;
now i hv tried to add lable bt not Succeeded.
plz help.
Amit ChoudharyPosted Mar 27, 2010, 5:37 AM
try this code:
Please mark as answer if it helps