|
I have to export the data present in the datagridview to Microsoft Excel. It is a window application using c#. Does anyone know how to do it. Please help.
Thanks
|
|
|
|
|
|
I have a method in a common base class thus :
protected bool SetFlag(bool _new, ref int _flags, int _flag, string _pName)
{
bool __cancelled = false;
bool __old = ((_flags & _flag) == _flag);
if (!__old.Equals(_new)
{
if (!(__cancelled = this.RaisePropertyChanging(__old, _new, pName)))
{
_flags = (_new) ? _flags | _flag : _flags & ~flag;
this.RaisePropertyChanged(__old, _new, pName);
}
}
return __cancelled;
}
In derivatives of the common base I want to use SetFlags as is shown in the example at the end of this post, but I get a message telling me that it can't cast a ref to an AmbientState to an int etc.
If I give SetFlag a Type argument then I can only constrain it to a class(reference type) or struct(value type), but in that case I get compile errors stating that bitwise operations cannot be performed on T objects, ie structs.
The content of _flags (ambientState in the example) must be changed BEFORE PropertyChanged handlers are invoked.
I have a work-around - The _flags parameter is not passed by reference and I've added a delegate method argument to SetFlag that gets invoked between the "flags = ...." and the "this.RaisePropertyChanged...." statements, the delegate looks like
private void saveFlags(int _flags) { this.ambientState = (AmbientState) _flags; )
But like all workarounds its ugly and necessitates documenting why the delegate exists etc.
Rgds PhilD -- Sample usage of SetFlags follows
public class MyClass : CommonBase
{
AmbientState ambientState = AmbientState.ZeroValue;
public bool Morning
{
get { return ((this.ambientState & AmbientState.Morning) == AmbientState.Morning); }
set { this.SetFlag(value, (int) ref this.ambientState, AmbientState.Morning, "Morning"; }
}
[Flags]
private enum AmbientState : int
{
ZeroValue = 0x0
Morning = 0x1
Afternoon = 0x2
Evening = 0x4
Night = 0x8
ClearSky = 0x10
CirrusSky = 0x02
etc
}
}
|
|
|
|
|
The most obvious solution is to set an int value, call the method with a ref to that value, then reset it to the ambientState property, as follows:
public bool Morning {
get { ... }
set {
int as = (int)this.ambientState;
this.SetFlag(value, ref as, ...);
this.ambientState = (AmbientState)as;
}
} Another solution I see is doing the reverse, which is probably better, since you cut down on the responsibility of the user, and to impede type checking for your enum type. Do this as follows:
protected bool SetFlag(bool _new, ref AmbientState _flags, AmbientState _flag, string _pName)
{
int flag = (int)_flag;
int flags = (int)_flags;
bool __cancelled = false;
bool __old = ((flags & flag) == flag);
if (!__old.Equals(_new)
{
if (!(__cancelled = this.RaisePropertyChanging(__old, _new, pName)))
{
flags = (_new) ? flags | flag : flags & ~flag;
this.RaisePropertyChanged(__old, _new, pName);
_flags = (AmbientState)flags;
}
}
return __cancelled;
} And finally, the RECOMMENDED way of doing this, is setting the FlagsAttribute on the enumeration as follows:
[FlagsAttribute]
public enum AmbientState {
...
} This final way allows you to use &, |, and ^ on the values of the enumeration. I'm not sure if you can use ~, but you can always go to find out! Hope this helps,
-Jeff
|
|
|
|
|
|
Hi you have some options.
1) use the fields in your datagrid. Attention, this method might be slow.
Just itterate throug your rows:
<br />
<br />
<br />
foreach (DataGridViewRow row in settingDataGridView.Rows)<br />
<br />
{ <br />
runningtotal += decimal.Parse(row.Cells[columnIndex].ToString();<br />
}<br />
2) get the data from the data behind the DataGridView
<br />
documentlinebindingsource.EndEdit();<br />
<br />
foreach (DataRow row in ds.DocumentLine.Select("documentId=" + documentId))<br />
<br />
{<br />
<br />
if (row.RowState != DataRowState.Deleted)<br />
<br />
{<br />
ds.DocumentLineRow docLineRow = (ds.DocumentLineRow)row;<br />
<br />
...<br />
runningtotal += row.documentLineTotal;<br />
<br />
}<br />
<br />
}<br />
<br />
Hope this will help you.
SSK.
Anyone who says sunshine brings happiness has never danced in the rain.
|
|
|
|
|
You can add a column to your datatable with autoincrement property set to true.This would give you serial number.I haven't tested it but it might work.
Until you realize this message has nothing to say, its too late to stop reading
|
|
|
|
|
hi
i am a beginner!
I need to know how to populate a single column from a table to a datagridview.
my code is
DataSet Ds=new Dataset()
........//here i retrived data from table and filled the dataset with data adapter.
da.fill(ds);
DatGridVw1.DataSource = ds;
DatGridVw1.DataBindings();
suggest me!
thanks in advance
kssk
|
|
|
|
|
The Databind is a method for the gridview for Asp .Net and databindings is to assign data to a control (this is usually a field value)
So remove the databindings line.
Are you sure that your dataset is getting populated?
Try running into debug mode and check the contents of your dataset before you set the data source of the gridview.
SSK.
Anyone who says sunshine brings happiness has never danced in the rain.
|
|
|
|
|
ya i checked my dataset, its populated.
but datagridview is empty
kssk
|
|
|
|
|
hi ,
if i may ask, what code your are writing for retrieval ?
if ur dataset is getting populated, then use : DataGridVw1.DataSource=ds.Tables[0];
and remove the data bindings line. and then check ?
|
|
|
|
|
Printing data in C#.NET from webbrowser used on windows form.
No other data will be printed except webbrowser
|
|
|
|
|
Hi, I have requirement like this..
I have one windows form which contains datagrid which displays the data.
And another button is there outside of grid in winform. If i click on button, the datagrid has to be printed...
so, i need to call window.print() in button click..
Ho can i call javascript in button click.
Thanks in advance
|
|
|
|
|
I have also faced the same situation i solved this by calling
Page_ClientValidate() function before printing. Don't directly call
window.print() instead
<br />
<br />
private void Page_Load(object sender, System.EventArgs e)<br />
{<br />
StringBuilder myScript = new StringBuilder();<br />
<br />
myScript.Append("<script language='Javascript'>");<br />
myScript.Append(" function PrintForm(){");<br />
myScript.Append(" if (Page_ClientValidate()){");<br />
myScript.Append(" window.print();}");<br />
myScript.Append("}");<br />
myScript.Append("</script>");<br />
<br />
this.RegisterStartupScript("MyScript", myScript.ToString());<br />
<br />
Button2.Attributes.Add("onclick","javascript:PrintForm();");<br />
}<br />
<br />
this should help.
SSK.
Anyone who says sunshine brings happiness has never danced in the rain.
|
|
|
|
|
My Requirement is in Windows form..not in webform.
In windows, we dont have Page_load , REgisterclientscriptblock etc..
Can u suggest me regarding windows form..
|
|
|
|
|
Hi,
you can check this...
Link1[^]
Thanks,
Sun Rays
To get something you must have to try once.
My Articles
|
|
|
|
|
Hi,
The code here is working very good when the ForBorderStyle is NOT set to "None":
http://www.codeproject.com/KB/vb/SnapForm.aspx
In my application, it's set to None... And I move the form by holding down the right mouse button and moving it. The code still partially work, but when I approach the taskbar from the clock side it's not completely sticked to the corners.
I tried to figure out what wrong with code but no luck (and no experience).
The complete code is attached. Just create a new project and paste it.
Your help would be greatly appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,
int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
# region SnapToDesktopBorder
private const int mSnapOffset = 35;
private const int WM_WINDOWPOSCHANGING = 70;
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public int flags;
}
protected override void WndProc(ref Message m)
{
// Listen for operating system messages
switch (m.Msg)
{
case WM_WINDOWPOSCHANGING:
{
SnapToDesktopBorder(this, m.LParam, 0);
}
break;
}
base.WndProc(ref m);
}
public static void SnapToDesktopBorder(Form clientForm, IntPtr LParam, int widthAdjustment)
{
if (clientForm == null)
{
// Satisfies rule: Validate parameters
throw new ArgumentNullException("clientForm");
}
// Snap client to the top, left, bottom or right desktop border
// as the form is moved near that border.
try
{
// Marshal the LPARAM value which is a WINDOWPOS struct
WINDOWPOS NewPosition = new WINDOWPOS();
NewPosition = (WINDOWPOS)System.Runtime.InteropServices.Marshal.PtrToStructure(LParam, typeof(WINDOWPOS));
if (NewPosition.y == 0 || NewPosition.x == 0)
{
return;
// Nothing to do!
}
// Adjust the client size for borders and caption bar
Rectangle ClientRect = clientForm.RectangleToScreen(clientForm.ClientRectangle);
ClientRect.Width += SystemInformation.FrameBorderSize.Width - widthAdjustment;
ClientRect.Height += (SystemInformation.FrameBorderSize.Height + SystemInformation.CaptionHeight);
// Now get the screen working area (without taskbar)
Rectangle WorkingRect = Screen.GetWorkingArea(clientForm.ClientRectangle);
// Left border
if (NewPosition.x >= WorkingRect.X - mSnapOffset && NewPosition.x <= WorkingRect.X + mSnapOffset)
{
NewPosition.x = WorkingRect.X;
}
// Get screen bounds and taskbar height
// (when taskbar is horizontal)
Rectangle ScreenRect = Screen.GetBounds(Screen.PrimaryScreen.Bounds);
int TaskbarHeight = ScreenRect.Height - WorkingRect.Height;
// Top border (check if taskbar is on top
// or bottom via WorkingRect.Y)
if (NewPosition.y >= -mSnapOffset && (WorkingRect.Y > 0 && NewPosition.y <= (TaskbarHeight + mSnapOffset)) || (WorkingRect.Y <= 0 && NewPosition.y <= (mSnapOffset)))
{
if (TaskbarHeight > 0)
{
NewPosition.y = WorkingRect.Y;
// Horizontal Taskbar
}
else
{
NewPosition.y = 0;
// Vertical Taskbar
}
}
// Right border
if (NewPosition.x + ClientRect.Width <= WorkingRect.Right + mSnapOffset && NewPosition.x + ClientRect.Width >= WorkingRect.Right - mSnapOffset)
{
NewPosition.x = WorkingRect.Right - (ClientRect.Width + SystemInformation.FrameBorderSize.Width);
}
// Bottom border
if (NewPosition.y + ClientRect.Height <= WorkingRect.Bottom + mSnapOffset && NewPosition.y + ClientRect.Height >= WorkingRect.Bottom - mSnapOffset)
{
NewPosition.y = WorkingRect.Bottom - (ClientRect.Height + SystemInformation.FrameBorderSize.Height);
}
// Marshal it back
System.Runtime.InteropServices.Marshal.StructureToPtr(NewPosition, LParam, true);
}
catch (ArgumentException ex)
{
}
}
# endregion
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
}
}
|
|
|
|
|
Hi,
Is it possible to record the voice from phone using tapi with C#.Net ?
Thanks,
Gopal.S
|
|
|
|
|
Yes.
SSK.
Anyone who says sunshine brings happiness has never danced in the rain.
|
|
|
|
|
Hi,
Thanks for your reply. Could you send any sample or material links for this one?
Regards,
Gopal.S
|
|
|
|
|
|
Hi friends...
is it possible to have a datagrid with multiple header?
i want to generate a report in datagrid.
the form should be like this
Heading 1 | Heading 2| Heading 3 | Main Heading
-----------|-----------|-----------|
Sh1 | Sh2 | Sh1 | Sh2 |SH1 | Sh 2 |Sub Heading
----|------|-----|-----|----|------|
|Rows......
-----------------------------------|
how is it done?
somebody plz help me....
|
|
|
|
|
|
I want to store the edit position of a richtextbox and set the position back after I renewed it's contents.
Thanks.
|
|
|
|
|
I believe the SelectionStart property stores where the caret is, if there's no selection.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|