|
Hi,
I have a DataTable bound to a DataGridView with this code:
dataGridView1.DataSource = myDT_Items;
I'm updating a DataGridView with this code:
dgvRow.Cells["ItemSize"].Value = 55;
Now I want the DataTable to reflect the changes made to the DataGridView. Currently I'm using:
myDT_Items.AcceptChanges();
Q: Is there another way to Synchronize DataTable with DataGridView when changes are made to the DataGridView?
Thanks,
Ron
|
|
|
|
|
Why are you using the synchronization? Are you using datagridview some where else as well?
If not then do the changes directly in the datatable.
Ahsan Ullah
Senior Software Engineer
|
|
|
|
|
Thanks Ahsan,
Yes, I could update the DataTable directly, but then I'd have use something like dataTable.Rows.Find() to correlate with the selected row in DataGridView. Do you know of a better way to find the DataTable row based on the selected DataGridView Row?
Even if I do go the route of updating the DataTable directly, I'm always interested in learning more. I'm basically wanting to know the best way to sync the DataTable with the DataGridView if the DataGridView is edited.
Ron
|
|
|
|
|
How can i create my own shortcut key with ctrl+y for saving file in ms visio using c# .net.
tahir
|
|
|
|
|
using System;
using System.Reflection;
namespace SqlSentence
{
public static class SqlString
{
public static string SELECT(this string This, params string[] columnName)
{
string s=" SELECT ";
for (int i = 0; i < columnName.Length; i++)
{
s += columnName[i] + ",";
}
return This + s.Substring(0, s.Length - 1);
}
public static string FROM(this string This, string tableName)
{
return This + " FROM " + tableName;
}
public static string AS(this string This, string target)
{
return This + " AS " + target;
}
public static string WHERE(this string This, string term)
{
return This + " WHERE " + term;
}
public static string EQUAL(this string This, string value)
{
return This + " = " + value;
}
public static string EQUAL(this string This)
{
return This + " = ";
}
public static string PARAMETERS(this string This, string value)
{
return This + "@" + value;
}
public static string AND(this string This, string where)
{
return This + " AND " + where;
}
}
}
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using SqlSentence;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmd.CommandText.SELECT("Name",
"sex".AS("Sex") , "ID").
FROM("customer").WHERE("name".EQUAL
("'zhangshan'").AND("sex".EQUAL
("'boy'")));
SqlCommand otherCmd = new SqlCommand();
otherCmd.CommandText = otherCmd.CommandText.
SELECT("name",
"sex".AS("Sex"),
"ID").
FROM("customer").
WHERE("name".EQUAL
("'zhangshan'").AND
("name".EQUAL().
PARAMETERS("value")));
}
}
}
|
|
|
|
|
Your code would be clearer if you simply wrote out the query; I don't think you gain anything by this technique.
@"
SELECT name
,sex AS Sex
,ID
FROM customer
WHERE name='zhangshan'
AND sex='boy'
"
And I don't see where you're adding the parameter in the second example.
|
|
|
|
|
I don't think is clearer like that
|
|
|
|
|
Good for you. Unfortunately the rest of us do NOT share the same enthusiasm as you
|
|
|
|
|
No, that's not like LINQ at all.
Slightly interresting attempt at building queries in a structured way using expansion, but it needs a lot more to be anything other than restricting. How about joins, grouping, sorting, subqueries, functions, operators...?
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi,
Is it possible to select and expand a treeNode in a treeView by program, with the treeNode highlighted ? I tried many methods, but none can get the treeNode highlighted while it expand ! I want to emulate the situation where user click the treeNode by program.
Thanks
Kim
|
|
|
|
|
You can do it by using SelecedNode.
example :
TreeView1.SelecedNode = TreeView1.Nodes[0].Nodes[4].Nodes[20];<br />
TreeView1.Focus();
Hello Friends
|
|
|
|
|
It works !
Thanks so much
|
|
|
|
|
You`re welcome
Hello Friends
|
|
|
|
|
Hi,
I just find a problem in getting the treeNode selected and highlighted if the treeView is in a panel rather than in the form directly, the treeNode cannot get the focus. I try to focus the panel also, but it didnt help.
Thanks
|
|
|
|
|
like this:
void print <T, G, F,...>(T t, G g, F f, ...);
print <int, string, double,...>(10, "hello", 10.01,...);
thanks!
|
|
|
|
|
What happened when you tried it ?
|
|
|
|
|
I am trying to insert a new record into my database. The primary key field is an integer. How can I determine what the max value for the field is before I do my insert?
I know this is very easy if I put the insert inside of a stored proc and just do a Max() before my insert, but for this project I can not use stored procs, all the sql is in the code. Also, I know I can set the primary key to be an identity field, but I am trying to avoid that.
Here is my current code:
public bool Insert(ActionObject obj)
{
SqlCommand sp;
try
{
using (SqlConnection conn = SqlConn)
{
conn.Open();
string sql = @"INSERT
INTO [dbo].[Action] ([ActionId], [ActionName], [ActionDescription])
Values (@ActionId, @ActionName, @ActionDescription)";
sp = new SqlCommand(sql, conn);
sp.CommandType = CommandType.Text;
sp.Parameters.Add("@ActionId", SqlDbType.Int).Value = obj.ActionId;
sp.Parameters.Add("@ActionName", SqlDbType.VarChar).Value = obj.ActionName == null ? "" : obj.ActionName;
sp.Parameters.Add("@ActionDescription", SqlDbType.VarChar).Value = obj.ActionDescription == null ? "" : obj.ActionDescription;
sp.ExecuteNonQuery();
}
}
catch(Exception)
{
throw;
}
}
|
|
|
|
|
If you make the primary key column an identity column then SQL Server will work it out for you. That way you don't have to calculate it in advance - and run the risk of someone else performing the same calculation and getting in there before you.
|
|
|
|
|
I agree that would be the best solution, unfortunately these tables are already created and I can not alter their structure.
|
|
|
|
|
Then to do it safely you need to use a transaction. Have a look at the SqlTransaction class.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Seconding what Colin said, if you go with doing an identity field, you will not have to be concerned about it.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
That would be a reason not to use integers for primary keys.
|
|
|
|
|
What is the alternative? GUIDs?
|
|
|
|
|
Yes, they solve this kind of problem and others (at the cost of space, sure, but we have plenty of that now ).
But you also say you can't change the table so that's not an option here.
After I hit send on the other post I remembered that back when I used Oracle (6) there were "sequences" or something that gave the "next integer" and when the company I worked at the time started using SQL Server (6) we no longer had those and apparently not "identities" either, so we cobbled up our own "sequences" -- a table that held the necessary information to get the "next integer" for a particular table.
That may be an ancient technique to resurrect in your case.
|
|
|
|
|
Chain the GetMaxValue and the Insert in the same transaction.
Do housekeeping on the transaction log, though.
|
|
|
|