How to bind gridview to another grid view? - c#

I am using 2 gridview in my project, i binded the employee on one grid and i want to show the orders of each employee on the second. i am stuck at filtering the other employees, when i hit the show orders button it shows me all the orders, how can i point at the right index(the employee selected)? .
My code:
private void Form3_Load(object sender, EventArgs e)
{
using (NorthWindDataContext db = new NorthWindDataContext())
{
var query =
from d in db.Employees
select new
{
d.FirstName
};
dataGridView1.DataSource = query;
}
}
private void displayOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
using (NorthWindDataContext db = new NorthWindDataContext())
{
var query =
from o in db.Orders
select new
{
o.ShipName <----problem here :(
};
dataGridView2.DataSource = query;
}
}
Please help
Thanks

Use GridView.SelectedValue Property.
You need to write something similar to:
private void displayOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
using (NorthWindDataContext db = new NorthWindDataContext())
{
var query =
from o in db.Orders.
Where(item => item.ID == dataGridView1.SelectedValue)
select new
{
o.ShipName <----problem here :(
};
dataGridView2.DataSource = query;
}
}

Related

c# ado.net cant save long strings to sql database

I'm trying to get an input as text and save it to SQL table,
the code works fine if the string isn't too long, but when you exceed from about 70 characters the SaveChanges(); command isn't able to save the data.
SQL data type is set to nvarchar(max). Any idea?
private void btnSave_Click(object sender, EventArgs e)
{
Model.TBL_USERS USR = new Model.TBL_USERS();
USR.USR_NAME = txtNAME.Text;
USR.USR_LAST = txtLAST.Text;
USR.USR_MOBILE = txtMOBILE.Text;
USR.USR_TEL1 = txtTEL1.Text ;
USR.USR_ADDRESS = txtADDRESS.Text;
USR.USR_COMPANY = txtCOMPANY.Text;
//USR.USR_COMPANY = "=chrome..69i57.13150j0j4&sourceid=chrome&ie=UTF-8";
DB.TBL_USERS.Add(USR);
DB.SaveChanges();
dataGridView1.DataSource = "";
dataGridView1.DataSource = DB.TBL_USERS.ToList();
}
this is the whole code in the solution :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Model.SAMA_User_CRM_V1 DB = new Model.SAMA_User_CRM_V1();
int IDGRID;
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = DB.TBL_USERS.ToList();
}
private void btnSave_Click(object sender, EventArgs e)
{
Model.TBL_USERS USR = new Model.TBL_USERS();
USR.USR_NAME = txtNAME.Text;
USR.USR_LAST = txtLAST.Text;
USR.USR_MOBILE = txtMOBILE.Text;
USR.USR_TEL1 = txtTEL1.Text ;
USR.USR_ADDRESS = txtADDRESS.Text;
USR.USR_COMPANY = txtCOMPANY.Text;
//USR.USR_COMPANY = "=chrome..69i57.13150j0j4&sourceid=chrome&ie=UTF-8";
DB.TBL_USERS.Add(USR);
DB.SaveChanges();
dataGridView1.DataSource = "";
dataGridView1.DataSource = DB.TBL_USERS.ToList();
}
private void btnDELETE_Click(object sender, EventArgs e)
{
var qdel = DB.TBL_USERS.Where(x => x.ID == IDGRID).ToList();
foreach (var item in qdel)
{
DB.TBL_USERS.Remove(item);
}
DB.SaveChanges();
dataGridView1.DataSource = "";
dataGridView1.DataSource = DB.TBL_USERS.ToList();
}
private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
IDGRID = (int)dataGridView1.Rows[e.RowIndex].Cells["ID"].Value;
}
}
}
considering the comments i found out where the problem is :
initially the model was created with a SQL table that used nvarchar(50) and after changing that to nvarchar(500) or nvarchar(max) the model.edmx needed to be updated ... after updating the model all things worked like a charm .

SelectedIndex is always 0 in dropdownlist(asp.net c#)

I am retrieving data from table Technology in dropdownlist ddlTechnology .
I have TechnologyId as primary key in the table and values are 1,2,3,4
Now as per the technology,i have to add questions in question bank. but when i select any item in dropdownlist, my SelectedIndex is always 0.
i want TechnologyId from dropdownlist.
i have tried following code but its not working
using (dbDataContext dt = new dbDataContext())
{
var qry = from i in dt.Technologies
select i;
ddlTechnology.DataSource = qry;
ddlTechnology.DataValueField = "TechnologyId";
ddlTechnology.DataTextField = "TechnologyName";
ddlTechnology.DataBind();
ddlTechnology.Items.Insert(0, new ListItem("Select Technology", ""));
}
Add button to add question according to selected technology.
protected void btnAdd_Click(object sender, EventArgs e)
{
using (dbDataContext dt = new dbDataContext())
{
Question objQtn = new Question();
objQtn.Question1 = txtQuestion.Text;
objQtn.Option1 = txtOption1.Text;
objQtn.Option2 = txtOption2.Text;
objQtn.Option3 = txtOption3.Text;
objQtn.Answer = txtAnswer.Text;
// below here selectedIndex is always zero..
objQtn.TechnologyId = ddlTechnology.SelectedIndex;
dt.Questions.InsertOnSubmit(objQtn);
dt.SubmitChanges();
txtAnswer.Text = "";
txtOption1.Text = "";
txtOption2.Text = "";
txtOption3.Text = "";
txtQuestion.Text = "";
}
}
Reason1:
Seems like you are binding dropdownlist on every postback. If that is a problem then keeping your load code in !IsPostBack shou work.
if(!IsPostBack)
{
using (dbDataContext dt = new dbDataContext())
{
var qry = from i in dt.Technologies
select i;
ddlTechnology.DataSource = qry;
ddlTechnology.DataValueField = "TechnologyId";
ddlTechnology.DataTextField = "TechnologyName";
ddlTechnology.DataBind();
ddlTechnology.Items.Insert(0, new ListItem("Select Technology", ""));
}
}
Reason 2:
In some cases if programmer disables ViewState property of any control/page then also control looses it's value on postback.
Need to bind dropdown list in the below event.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Bind your dropdown list
}
}
Please find the code for ref. hope it will help you:
(objQtn.TechnologyId = ddlTechnology.SelectedIndex;)
one of these two is of string type check once.
Index.aspx
<asp:DropDownList ID="ddlCloth" runat="server"></asp:DropDownList>
<asp:Button runat="server" ID="btnSave" OnClick="btnSave_Click" />
Index.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[2] { new DataColumn("Id"), new DataColumn("Name") });
dt1.Rows.Add(1, "Shirt");
dt1.Rows.Add(2, "Jeans");
ddlCloth.DataSource = dt1;
ddlCloth.DataTextField = "Name";
ddlCloth.DataValueField = "Id";
ddlCloth.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(ddlCloth.SelectedItem.Value);
string text = Convert.ToString(ddlCloth.SelectedItem.Text);
int index=Convert.ToInt32(ddlCloth.SelectedIndex);
}

Show tables using entity framework

I am trying to show all the data of t1 table in a gridview using Entity Framework but I am getting an error
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
This is my code:
protected void Button2_Click(object sender, EventArgs e)
{
var v = (from obj in de.t1
where obj.Id == Convert.ToInt32(TextBox5.Text)
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
Entity Framework is trying to execute Convert.ToInt32(TextBox5.Text) on the SQL side, which it obviously cannot do.
So, introduce a temporary variable to store the result of conversion:
var tempId = Convert.ToInt32(TextBox5.Text);
Then pass it to the where clause:
...
where obj.Id == tempId
...
The problem you are running into is the fact that the entity framework doesn't know how to translate Convert.ToInt32 into a valid SQL expression. Simply cast the textbox value to an integer before you query, and things should be fine.
protected void Button2_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBox5.Text);
var v = (from obj in de.t1
where obj.Id == id
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
var objId = Convert.ToInt32(TextBox5.Text);
var v = (from obj in de.t1
where obj.Id == objId
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
var id = Convert.ToInt32(TextBox5.Text);
var v = (from obj in de.t1
where obj.Id == id
select obj).ToList();
GridView1.DataSource = v;
GridView1.DataBind();
}

Multi Select rows datagridview and insert into table

I have a datagridvie that is populated using the following:
private void txtStreet_Leave(object sender, EventArgs e)
{
DataClasses1DataContext db = new DataClasses1DataContext();
var groups = (from c in db.GetTable<locality>()
where c.Number.Contains(txtNum.Text)
&& c.street.Contains(txtStreet.Text)
select new { c.Number, c.street, c.LienNumber, c.LienType, c.Amount } );
dataGridView1.DataSource = groups;
}
I then need to be able to multi select the records that match the my criteria and then add them to a table. I have started doing that with the code below. My problem is extracting the values from the selected rows and then updating the correct table with the rows. Is using a list the wrong route?
public void btnAddLocs_Click(object sender, EventArgs e)
{
List<DataGridViewRow> rowCollection = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
DataClasses1DataContext db = new DataClasses1DataContext();
MuniLien newlien = new MuniLien();
newlien.CaseNumberKey = _owner.caseNumberKeyTextBox.Text;
newlien.LienAmt =
newlien.LienDate =
newlien.LienReason =
newlien.LienNumber =
db.MuniLiens.InsertOnSubmit(newlien);
db.SubmitChanges();
}
}
I think that using DataGridRowCollection is a better approach(the DataGrid.Rows property is this type).
So you simply add a DataGridRow to the list of rows and thats all and its fully foreach capable.
Checkout http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrowcollection(v=vs.110).aspx
Hope this helps!

Datagridview insert data not show

I just learn linq and trying to make insert update delete with linq and ado.net data entity model. Now I'm have some problem, when I'm insert some data, the datagridview not show that, I must close the program and run it again to show the latest data that I'm entry. Here the code :
namespace TesDB
{
public partial class Form1 : Form
{
private Database1Entities de = new Database1Entities();
int index;
public Form1()
{
InitializeComponent();
var query = from x in de.MsEmployees
select x;
dataGridView1.DataSource = query;
dataGridView1.Refresh();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void loadtext()
{
textBox1.Text = dataGridView1.Rows[index].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[index].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[index].Cells[2].Value.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
string EmployeeID = dataGridView1.Rows[index].Cells[0].Value.ToString();
var employee = (from x in de.MsEmployees
where x.EmployeeID == EmployeeID
select x).First();
employee.EmployeeID = textBox1.Text;
employee.EmployeeName = textBox2.Text;
employee.Salary = int.Parse(textBox3.Text);
de.SaveChanges();
Refresh();
}
private void button3_Click(object sender, EventArgs e)
{
string EmployeeID = dataGridView1.Rows[index].Cells[0].Value.ToString();
// int id1 = int.Parse(EmployeeID);
var Employee = (from x in de.MsEmployees
where x.EmployeeID == EmployeeID
select x).First();
de.MsEmployees.DeleteObject(Employee);
de.SaveChanges();
Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
string employeeID = dataGridView1.Rows[index].Cells[0].Value.ToString();
var employee = (from x in de.MsEmployees
where x.EmployeeID == employeeID
select x.EmployeeID).First();
MsEmployee me = new MsEmployee();
me.EmployeeID = textBox1.Text;
me.EmployeeName = textBox2.Text;
me.Salary = int.Parse(textBox3.Text);
de.AddToMsEmployees(me);
de.SaveChanges();
this.Refresh();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
index = e.RowIndex;
loadtext();
}
}
}
`
I'm using refresh() that work for Update and Delete but it not work for insert does anyone know how to refresh that table when I'm done insert?
make a method of refresh() and paste ur Query inside and call it behind the button
public void refresh()
{
//Paste ur Query here and call it behind the button_click Event
}

Categories