**hello
Error adding items to the Grid.
please guide me**
Model1Container Mobl = new Model1Container();
JadvalSabtenam Sabt = new JadvalSabtenam();
Sabt.name = TextBox1.Text;
Sabt.family = TextBox2.Text;
Mobl.AddToJadvalSabtenamSet(Sabt);
Mobl.SaveChanges();
GridView1.DataSource = Sabt;
GridView1.DataBind();
GridView1.DataSource = Sabt;
GridView1.DataBind();
Sabt seems to be single object. To bind DataGrid you need to have collection like List<JadvalSabtenam > or BindingList<JadvalSabtenam>
Try that:
List<JadvalSabtenam > dataSource = new List<JadvalSabtenam> {Sabt};
GridView1.DataSource = dataSource;
GridView1.DataBind();
Reason for that is: What Grid should do with single object? When it's collection each item is corresponding to one row, and each property to column which makes perfect sense.
thank you. very well.
My problem was solved with your code.
List<JadvalSabtenam> dataSource = new List<JadvalSabtenam> { Sabt };
GridView1.DataSourceID = "";
GridView1.DataBind();
Related
RDP0 yeni = new RDP0();
yeni.IPADDRESS = "1.1.1.1";
yeni.PASSWORD = " 10akh0";
yeni.NO = 8;
yeni.CUSTID = 1000;
testList.Add(yeni);
dataGridView1.DataSource = testList;
I'm getting data-bound error therefore I cannot add rows. Any help would be great.
This is how I bound data to my dataGV:
tm = new testModel();
var query = from fe in tm.ERDP0 select fe;
testList = query.ToList();
dataGridView1.DataSource = testList;
If the DataGridView is data bound you cannot add new rows/columns to the DataGridView directly. But you could add a row to a DataTable which will update your DataGridView automatically.
DataTable dt = dataGridView1.DataSource as DataTable;
dt.Rows.Add("new row");
Or you could add the new row to your source list and then reset the DataSource:
testList.Add(youritem);
dataGridView1.DataSource = null;
dataGridView1.DataSource = testList;
I am displaying values from a database into a dropdownlist. but I am unable to get the corresponding values into the DDL.
if (dt.Rows.Count > 0)
{
DataTable dt1 = new DataTable();
dt1 = bll.getnewid(TextBox1.Text);
if (dt1.Rows.Count > 0)
{
Session["pid"] = dt1.Rows[0]["NewidColumn"].ToString();
Session["email"] = dt1.Rows[0]["EmailID"].ToString();
Session["gender"] = dt1.Rows[0]["Gender"].ToString();
Session["mobile"] = dt1.Rows[0]["MobileNo"].ToString();
Session["country"] = dt1.Rows[0]["Country"].ToString();
Session["state"] = dt1.Rows[0]["State"].ToString();
}
and I am displaying like this
DropDownList1.Text = Session["country"].ToString();
DropDownList2.Text = Session["state"].ToString();
I am able to get the country and state values in the datatable. but I am unable to display them in the DDL.
DropDownList1.Items.Add(new ListItem(Session["country"].ToString());
DropDownList2.Items.Add(new ListItem(Session["state"].ToString());
Dropdownlist2.databind();
Dropdownlist1.databind();
DropDownList1.Items.Add(new ListItem(Session["country"].ToString());
DropDownList2.Items.Add(new ListItem(Session["state"].ToString());
You need to set the SelectedValue or SelectedIndex property of your dropdownlist.
try like this
DropDownList1.Items.Add("yourvalue");
DropDownList1.Items.Add(Session["country"].ToString());
Try This Code :
DropDownList1.Items.Insert(0, new ListItem(Session["country"].ToString(), "0"));
DropDownList1.DataBind();
DropDownList2.Items.Insert(0, new ListItem(Session["state"].ToString(), "0"));
DropDownList2.DataBind();
What is the need of using sessions ..?
Instead try this..hope it works.
After this line of code (dt1 = bll.getnewid(TextBox1.Text);)
use this two lines instead of sessions.
DropDownList1.DataSource=dt1;
DropDownList1.DataTextField="Country";
DropDownList1.DataValueField="Country";
DropDownList1.DataBind();
DropDownList2.DataSource=dt1;
DropDownList2.DataValueField="Country";
DropDownList2.DataValueField="Country";
DropDownList2.DataBind();
i want to fill a combobox with data from the database when the page load
I had written the code as below
private void QuotationForm_Load(object sender, EventArgs e)
{
MessageBox.Show("hghjgvhg");
comboboxload();
}
public void comboboxload()
{
OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
oleDbConnection1.Open();
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobecode from jobcodemastertable",oleDbConnection1);
OleDbDataReader reader = oleDbCommand1.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("jobpk", typeof(int));
dt.Columns.Add("jobcode", typeof(string));
dt.Load(reader);
cmbjobcode.ValueMember = "jobpk";
cmbjobcode.DisplayMember = "jobcode";
cmbjobcode.DataSource = dt;
oleDbConnection1.Close();
}
it doesnot deturns an error or exception but doesnot load the combobox with data values
try this
comboBox1.DataSource = ds.Tables[0];
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
You may need to bind datatable's view with combo box
cmbjobcode.DataSource = dt.DefaultView;
You're missing the DataBind method
dt.Load(reader);
cmbjobcode.ValueMember = "jobpk";
cmbjobcode.DisplayMember = "jobcode";
cmbjobcode.DataSource = dt;
//here
cmbjobcode.DataBind();
oleDbConnection1.Close();
You have to call DataBind method on your combo. Thats why its not populating.
I have a session object that contains a DataTable from my previous page,
and i would like to bind this DataTable to a ListBox.
I'v done this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["bestStocks"] !=null)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["bestStocks"];
DataView dv = new DataView(dt);
BestStockslb.DataSource = dt;
BestStockslb.DataBind();
}
}
}
I get this result:
Any suggestion?
thanks,
liron
It seems you have forgot the DataTextField and DataValueField
dt = (DataTable)Session["bestStocks"];
DataView dv = new DataView(dt);
BestStockslb.DataSource = dt;
BestStockslb.DataTextField = "Name";
BestStockslb.DataValueField = "ID";
BestStockslb.DataBind();
Change this line:
BestStockslb.DataSource = dt;
To:
BestStockslb.DataSource = dt.DefaultView;
And you also need to set the DataTextField and DataValueField Properties of BestStockslb to link to the required fields in the returned data. This is why you are getting the DataRowView output.
You could also remove DataView dv = new DataView(dt); as it looks like you are not using it.
I have no idea where to start. i tried DataTable but it didn't work.(This is an easy question :) )
I tried everything
{
var test = new DataTable();
test.Columns.Add("test");
test.TableName = "test";
test.Columns.Add("test");
comboBox1.DataSource = test.XXXX ;
}
Assuming you mean winforms, something like:
DataTable test = new DataTable();
test.TableName = "test";
test.Columns.Add("foo", typeof(string));
test.Columns.Add("bar", typeof(int));
test.Rows.Add("abc", 123);
test.Rows.Add("def", 456);
ComboBox cbo = new ComboBox();
cbo.DataSource = test;
cbo.DisplayMember = "foo";
cbo.ValueMember = "bar";
Form form = new Form();
form.Controls.Add(cbo);
Application.Run(form);
(in particular, SelectedValue should give you the 123 and 456 - useful for ids, etc)
ComboBox.Items property, unless you want data from a database or something.
DataTable dt=new DataTable();
dt.Columns.Add("Col1",typeof(int));
dt.Columns.Add("Col2",typeof(String));
dt.Rows.Add(1,"A");
dt.Rows.Add(2,"B");
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Col2";
comboBox1.ValueMember = "Col1";
You'll need to set the 'DataItemField' and 'DataValueField' to the appropriate column names in your datatable.