|
Generally,we can control datalist's display field by databinding by html,such as <%# Container.DataItem("Title") %>
Now i have a problem,I want to change this bind field by my option in codebehind file,such as i want to display Title+Digest or Column+Title
so who can tell me how to make this done?
Thanks in advance!
I'm terribly troubled by this problem and looking forward to your reply.
|
|
|
|
|
Hi,
simple question I expect, it's just so hard to find out how to do things...
I have a WinForm with a DataGrid which gets its data from an XML file. I would like the user to be able to edit the data and save it with a button click.
(straightforward huh?)
Any help anyone?
Thanks, J
|
|
|
|
|
Hi, J. Does this work for you?
void SaveDataGrid(DataGrid dg, string sFile)
{
DataSet ds = dg.DataSource;
ds.WriteXml(sFile);
}
|
|
|
|
|
no it doesn't unfortunately.
i get the following:
Cannot implicitly convert type 'object' to 'System.Data.DataSet'
(think it using the older framework, ie not 1.1 - could that make a difference?)
|
|
|
|
|
Try casting the source to DataSet
<br />
DataSet ds = dg.DataSource;<br />
if that doesn't work check the type of the DataSource and get back to us.
<br />
string typeName = dg.DataSource.GetType().ToString();<br />
The smaller the mind the greater the conceit.
Aesop
|
|
|
|
|
the casting worked fine, don't know why i didn't try that before. thank you.
(it was type dataset)
|
|
|
|
|
Yea, I forgot about the casting. DataSource is an Object property (it can accept several types of objects) so it makes sense to cast it to a DataSet first.
|
|
|
|
|
The following is my program of sending an email, when i debug it with F10 step by step, it seems that every thing goes right, and the receiver can receive the mail.
but when i run the program, there are always problems and the mail can't be delivered....
please help me!
using System;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Data;
using System.Net;
using System.Threading;
namespace MailSend
{
/// <summary>
/// MailSend 的摘要说明。
/// </summary>
public class MailSend : TcpClient
{
public String server;
public int port = 25;
public String username = "xxxx";
public String password = "xxxx";
public String subject = "the mail subject";
public String htmlbody = "<HTML><HEAD><TITLE>Untitled Document</TITLE></HEAD> <body> <B> blank for now</html>";
public String from = "xxx@xxx.edu.cn";
public String to = "wolft@sina.com.cn";
public String fromname = "fromme";
public String content_type = "text/html";
public String encode = "base64";
public String charset = "GB2312";
string CRLF = "\r\n";
[STAThread]
static void Main(string [] args)
{
MailSend Ms = new MailSend();
Ms.server = "smtp.sjtu.edu.cn";
Ms.to = "wolft@sina.com.cn";
Ms.getMailServer();
if (Ms.send() == true)
{
Console.WriteLine("Mail delivered succesfully!");
Ms.Dispose(true);
Console.Read();
return;
}
Console.WriteLine("Mail UNdelivered!");
Ms.Dispose(true);
Console.Read();
}
public MailSend()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public bool send()
{
WriteStream("MAIL From: "+this.fromname+"<"+this.from+">" + CRLF);//发件人
WriteStream("RCPT To:"+this.to);//收件人
if (!OperaStream("DATA","354"))//
{
this.Close(); //关闭连接
return false;
}
WriteStream("Date: "+DateTime.Now); //下面这包东西一定要写在DATA后面,不然就发不出去了.
WriteStream("Subject: "+this.subject);//主题
WriteStream("To:"+this.to);//收件人
WriteStream("Content-Type: text/html");
WriteStream(htmlbody);
WriteStream(CRLF);
if (!OperaStream(".","250"))//最后写完了,输入"."
{
WriteStream(CRLF);
this.Close(); //关闭连接
return false;
}
WriteStream(CRLF);
return true;
}
private void WriteStream(String strCmd)
{
Stream TcpStream;
strCmd = strCmd + "\r\n";
TcpStream = this.GetStream();
byte [] bWrite = Encoding.GetEncoding("GB2312").GetBytes(strCmd.ToCharArray());
int start = 0;
int length = bWrite.Length;
int page = 0;
int size = 75;
int count = size;
if (length>75)
{
if((length/size)*size != length)
page = length/size + 1;
else
page = length / size;
for (int i = 0; i < page; i ++)
{
start = i * size;
if( i == page -1 )
count = length - ( i * size);
TcpStream.Write(bWrite, start, count);
}
}
else
TcpStream.Write(bWrite, 0, bWrite.Length);
}
private string ReceiveStream()
{
String sp = null;
byte [] by = new byte[1024];
NetworkStream ns = this.GetStream();
int size = ns.Read(by, 0, by.Length);
if (size>0)
{
sp = Encoding.Default.GetString(by);
}
return sp;
}
private bool OperaStream(string strCmd, string state)
{
string sp = null;
bool success = false;
try
{
WriteStream(strCmd);
sp = ReceiveStream();
if ( sp.IndexOf(state) != -1)
success = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
return success;
}
private string AuthStream(String strCmd)
{
try
{
byte [] by = Encoding.Default.GetBytes(strCmd.ToCharArray());
strCmd = Convert.ToBase64String(by);
}
catch(Exception ex)
{
return ex.ToString();
}
return strCmd;
}
public bool getMailServer()
{
try
{
System.Net.IPAddress ipaddress = (IPAddress)System.Net.Dns.Resolve(this.server).AddressList.GetValue(0);
System.Net.IPEndPoint endpoint = new IPEndPoint(ipaddress, 25);
Connect(endpoint);
ReceiveStream();
if (this.username != null)
{
if (!OperaStream("HELO Localhost", "250"))
{
this.Close();
return false;
}
if (!OperaStream("EHLO Localhost", "250"))
{
this.Close();
return false;
}
if (!OperaStream("auth login", "334"))
{
this.Close();
return false;
}
username = AuthStream(username);
if(!OperaStream(this.username, "334"))
{
this.Close();
return false;
}
password = AuthStream(password);
if(!OperaStream(this.password, "235"))
{
this.Close();
return false;
}
return true;
}
else
{
if(!OperaStream("EHLO Localhost", "250"))
{
return true;
}
else
return false;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
}
}
|
|
|
|
|
Some ideas:
* There could be a timing issue related to threading involved with the TcpClient. To test this, comment out the call to MS.Dispose(), see if the length of time that you sit at Console.Read() makes a difference.
* Put break points on your return statements, to see which one is failing. That way you can just jump to the error, rather than stepping into it. If timing is the issue, that should let you narrow down the problem.
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Does anybody know of a way to convert an INT to it binary representation?
i.e.
85 = 1010101
56 = 0111000
and so on
post.mode = postmodes.signature;
SELECT everything FROM everywhere WHERE something = something_else;
> 1 Row Returned
> 42
|
|
|
|
|
Try checking out the System.Collections.BitArray class; it provides a pretty decent way of looking at/manipulating integers at the bit-level. I've used it a few times for win32 and mp3 stuff.
Also, if you only want to check the presence/abscence of a particular bit, use the '&' bitwise operator. I've found it's extremely worthwhile to truly understand the bit-level operators, especially if you're doing those type of operations frequently.
|
|
|
|
|
int intSomeNumber = 999;
string strBits = Convert.ToString( intSomeNumber, 2 );
Michael Flanakin
Web Log
|
|
|
|
|
Hey guys(first datagrid app and form app, be nice :p ), I am trying to bind a datatable to a datasource to a Datagrid and its not working...Im really confused. This is the compiler error I keep getting.
MyFormProject.MainForm.DataSource denotes a 'field' where a 'class' was expected.
-and-
MyFormProject.MainForm.DataGrid denotes a 'field' where a 'class' was expected.
the error line for 1st one is : ds.Tables.Add(dt);
the error line for 2nd one is : dg.SetDataBinding(ds,"Items");
ds is declared: private System.Data.DataSet ds;
dg is declared: private System.Windows.Forms.DataGrid dg;
Where this occurs:
public static void createset(database obj){
System.Data.DataTable dt = new DataTable("Items");
DataRow dr;
DataColumn dc;
int rowcount = obj.size;
System.Type typeint;
typeint = System.Type.GetType("System.Int32");
System.Type typestring;
typestring = System.Type.GetType("System.Int32");
dc = new DataColumn();
dc = new DataColumn("ID",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Name",typestring);
dt.Columns.Add(dc);
dc = new DataColumn("Type",typestring);
dt.Columns.Add(dc);
dc = new DataColumn("Buy Price",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Sell Price",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Weight",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Attack",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Defense",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Range",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Slot",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Gender",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Loc",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Weapon Level",typeint);
dt.Columns.Add(dc);
dc = new DataColumn("Player Level",typeint);
dt.Columns.Add(dc);
ds.Tables.Add(dt);
for(int i=0;i
|
|
|
|
|
Not sure how much help this will be but when I have needed to create datatables within code I tend to use the follow :
private void DataGen()
{
DataTable gridDt = new DataTable("grid table");
gridDt.Columns.Add("INT", System.Type.GetType("System.Int32"));
gridDt.Columns.Add("STRING", System.Type.GetType("System.String"));
gridDt.Columns.Add("LONG", System.Type.GetType("System.Double"));
for (int i=0; i<30; i++)
{
object[] rowData = new object[] { i, i.ToString(), DateTime.Now.Ticks };
gridDt.Rows.Add(rowData);
}
dataGrid1.DataSource = gridDt;
}
post.mode = postmodes.signature;
SELECT everything FROM everywhere WHERE something = something_else;
> 1 Row Returned
> 42
|
|
|
|
|
Well I found the solution, and even tho it works perfectly I dont understand why it would not work before. All I changed was the dg.SetDataBinding thing by removing it and changing the return type from void to DataTable and then just saying that dg.DataSource = createset(database); Why does that make it work?!?! So confused.
|
|
|
|
|
I have several complex user controls built in separate C# projects that need a common interface. In the same solution is the container form where each control is attached. I've tried adding a class library project, with the needed interface, and adding the class library name to each of the projects references that need to inherit. The solution compiles but does not run. Any ideas?
Mel
|
|
|
|
|
Do you have an error message? If not, put a try/catch block around everything inside your Main() function, something like this:
public static void Main()
{
try
{
}
catch (Exception err)
{
System.Diagnostics.Trace.WriteLine(err.ToString());
MessageBox.Show(err.ToString());
}
}
Then, you'll have an error message that will help you diagnose the problem yourself.
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
I've run into a problem with .NET remoting. I need the client to instantiate an object on the server, however, intantiating that object requires I pass parameters to the constructor.
public class MyObj
{
public MyObj(string s, long l, SR someReferenceType)
{
}
}
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyObj), "MyURI", WellKnownObjectMode.Singleton);
RemotingServices.Connect(typeof(MyObj), "http://localhost/MyURI");
The RemotingServices.Connect call throws an exception because there is no default constructor. I need to somehow pass several value types and a remote object reference type to the object being constructed (it'd even be ok if I passed this data to the object after construction, but that doesn't seem possible either).
Any ideas?
---------------------------
He who knows that enough is enough will always have enough.
-Lao Tsu
|
|
|
|
|
Create your own RealProxy derivative that can incercept the construction calls and instantiate your class with the parameters provided. Should work since you're now the one controlling how objects are created now.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the reply. I have now created a RealProxy derivative for my type. I've designed the RealProxy about as closesly as I could to some MSDN examples...is there anything else I need to do to make this work? I assume I've gotta hook up that proxy when I call .Connect to get the proxy on the client or similar, is that right?
---------------------------
He who knows that enough is enough will always have enough.
-Lao Tsu
|
|
|
|
|
You actually don't have to handle as much as you might think when creating your own proxy. You can choose to handle which methods you want. I recommend - if you haven't already - picking up a good book on .NET Remoting like "Microsoft .NET Remoting" from MS Press[^] or the one from Ingo Rammer[^] (supposed to be very good).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Look at the code: the are two similar methods. One declares all variables used in the method at the start of the method, the other one declares them when it needs then. May there be any lower CPU or memory cost in the second version of the method (the one at the bottom) ?
<br />
public void DoSomethingComplex(...)<br />
{<br />
int[] intarray = new ...<br />
bool somecondition = ...<br />
if (somecondition == true)<br />
intarray[1].Add ...
}<br />
<br />
public void DoSomethingComplex(...)<br />
{<br />
bool somecondition = ...<br />
if (somecondition == true)<br />
{<br />
int[] intarray = new ...<br />
intarray[1].Add ...
}<br />
}
Regards, Desmond
|
|
|
|
|
It's not really the declarations of things you have to worry about in general from a performance perspective; it's the allocation of things. Check this out:
<br />
using System;<br />
<br />
class Test {<br />
<br />
<br />
public static void DoSomethingComplex(bool _someCondition) {<br />
int[] intArray;<br />
<br />
if (_someCondition) {<br />
intArray = new int[100];<br />
}<br />
<br />
}<br />
<br />
static void Main(string[] args) {<br />
DateTime startTime, endTime;<br />
int millisecondCount;<br />
int loopCount = 100000;<br />
<br />
startTime = DateTime.Now;<br />
for(int x = 0; x < loopCount; x++) {<br />
DoSomethingComplex(true);<br />
DoSomethingComplex(true);<br />
DoSomethingComplex(true);<br />
DoSomethingComplex(true);<br />
DoSomethingComplex(true);<br />
DoSomethingComplex(true);<br />
DoSomethingComplex(true);<br />
DoSomethingComplex(true);<br />
DoSomethingComplex(true);<br />
DoSomethingComplex(true);<br />
}<br />
endTime = DateTime.Now;<br />
millisecondCount = ((int)(endTime.Ticks - startTime.Ticks)) / 10000;<br />
Console.WriteLine(millisecondCount + " milliseconds" );<br />
<br />
startTime = DateTime.Now;<br />
for(int x = 0; x < loopCount; x++) {<br />
DoSomethingComplex(false);<br />
DoSomethingComplex(false);<br />
DoSomethingComplex(false);<br />
DoSomethingComplex(false);<br />
DoSomethingComplex(false);<br />
DoSomethingComplex(false);<br />
DoSomethingComplex(false);<br />
DoSomethingComplex(false);<br />
DoSomethingComplex(false);<br />
DoSomethingComplex(false);<br />
}<br />
endTime = DateTime.Now;<br />
millisecondCount = ((int)(endTime.Ticks - startTime.Ticks)) / 10000;<br />
Console.WriteLine(millisecondCount + " milliseconds" );<br />
<br />
}<br />
}<br />
|
|
|
|
|
The second version is better since you allocate the memory only if
if (somecondition == true)
Howver you can do the following:
<br />
public void DoSomethingComplex(...)<br />
{<br />
int[] intarray;
bool somecondition = ...<br />
if (somecondition == true)<br />
intarray = new ...
intarray[1].Add ...<br />
}
|
|
|
|
|
Unless you are into really performance critical applications (and if you are, why aren't you using C++?) I think good coding style and design is generally more important than worrying about how questions like this.
I prefer to declare locals at the point where I going to use them, not detached at the head of the function. To me this localises the use of the variable, and is just better style. Anyway, we shouldn't try to second guess the compiler - for all we know, the compiled code is near identical in both cases.
|
|
|
|