Click here to Skip to main content
16,021,432 members
Home / Discussions / C#
   

C#

 
QuestionDataGridView Pin
vhassan6-Aug-09 18:26
vhassan6-Aug-09 18:26 
AnswerRe: DataGridView Pin
Mycroft Holmes6-Aug-09 20:14
professionalMycroft Holmes6-Aug-09 20:14 
Questionon enter fire button Pin
Rajee Maharjan6-Aug-09 18:26
Rajee Maharjan6-Aug-09 18:26 
AnswerRe: on enter fire button Pin
MumbleB6-Aug-09 19:19
MumbleB6-Aug-09 19:19 
AnswerRe: on enter fire button Pin
Cracked-Down6-Aug-09 19:21
Cracked-Down6-Aug-09 19:21 
AnswerRe: on enter fire button Pin
Rajee Maharjan6-Aug-09 19:51
Rajee Maharjan6-Aug-09 19:51 
AnswerRe: on enter fire button Pin
DaveyM696-Aug-09 21:29
professionalDaveyM696-Aug-09 21:29 
QuestionHow to save the property of the user-defined column ? Pin
mctramp1686-Aug-09 17:39
mctramp1686-Aug-09 17:39 
public class TXDataGridViewNumericColumn : DataGridViewColumn
{
public TXDataGridViewNumericColumn() : base(new TXNumericCell())
{
this.InputType = NumericType.Integer;
}

public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(TXNumericCell)))
{
throw new InvalidCastException("Must be a CalendarCell");
}
base.CellTemplate = value;
}

}

private NumericType m_NumericType;

public enum NumericType
{
Decimal,
Integer,
PositiveDecimal,
NegativeDecimal,
PositiveInteger,
NegativeInteger
}

public NumericType InputType
{
get { return m_NumericType; }
set { m_NumericType = value; }
}

public ICollection testCollection
{
get
{
return Icollection;
}
set
{
Icollection = value;
}
}
private ICollection Icollection;


public override object Clone()
{
DataGridViewColumn col = (DataGridViewColumn)base.Clone();
col.CellTemplate = new TXNumericCell();
return col;
}

}

public class TXNumericCell : DataGridViewTextBoxCell
{

public TXNumericCell() : base()
{
}

public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
TXNumericEditControl ctl = DataGridView.EditingControl as TXNumericEditControl ;
ctl.Text = (string )this.Value;
}

public override Type EditType
{
get
{
return typeof(TXNumericEditControl);
}
}

public override Type ValueType
{
get
{
return typeof(string);
}
}

public override object DefaultNewRowValue
{
get
{
return string.Empty;
}
}
}

class TXNumericEditControl :TXNumeric , IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;

public TXNumericEditControl()
{
TXDataGridViewNumericColumn current = new TXDataGridViewNumericColumn();
this.InputType =(NumericType)current.InputType;
}

public object EditingControlFormattedValue
{
get
{
return this.Text ;
}
set
{
String newValue = value as String;
if (newValue != null)
{
this.Text = newValue;
}
}
}

public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}

public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}

public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}

public bool EditingControlWantsInputKey( Keys key, bool dataGridViewWantsInputKey)
{
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}

public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}


public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}


public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}


public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}

public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}

protected override void OnTextChanged(EventArgs e)
{
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnTextChanged(e);
}

}



public partial class TXNumeric : TextBox
{
private NumericType m_NumericType;

public enum NumericType
{
Decimal,
Integer,
PositiveDecimal,
NegativeDecimal,
PositiveInteger,
NegativeInteger
}

public TXNumeric()
{
this.InputType = NumericType.Integer;
this.ContextMenu = new ContextMenu();//屏蔽系统的菜单

}

public NumericType InputType
{
get { return m_NumericType; }
set { m_NumericType = value; }
}

#region user-defined method
private bool IsValid(string val, bool user)
{
bool ret = true;

if (val.Equals("") || val.Equals(String.Empty))
return ret;

if (user)
{
if (val.Equals("-"))
return ret;
}
try
{
switch (m_NumericType)
{

case NumericType.Decimal:
ret=this.IsValidNumeric(val);
break;
case NumericType.PositiveDecimal:
ret=this.IsValidPositiveNum(val);
break;
case NumericType.NegativeDecimal:
ret = this.IsValidNegativeNum(val);
break;
case NumericType.Integer:
ret = this.IsValidInt(val);
break;
case NumericType.PositiveInteger:
ret = this.IsValidPositiveInt(val);
break;
case NumericType.NegativeInteger:
ret = this.IsValidNegativeInt(val);
break;
//default:
// throw new ApplicationException();
}
}
catch
{
ret = false;
}
return ret;
}

/// <summary>
/// judge data if it is numeric type
/// </summary>
/// <param name="strData"></param>
/// <returns>bool</returns>
private bool IsValidInt(string strData)
{
int intData;
try
{
intData = Convert.ToInt32(strData);
return true;
}
catch
{
return false;
}

}

private bool IsValidPositiveInt(string strData)
{
int intData;
try
{
intData = Convert.ToInt32(strData);
if (intData > 0)
{ return true; }
else
{ return false; }

}
catch
{
return false;
}
}

private bool IsValidNegativeInt(string strData)
{
int intData;
try
{
intData = Convert.ToInt32(strData);
if (intData < 0)
{ return true; }
else
{ return false; }
}
catch
{
return false;
}
}

private bool IsValidNumeric(string strData)
{
double dblData;
try
{

dblData = Convert.ToDouble(strData);
return true;
}
catch
{
return false;
}
}

private bool IsValidPositiveNum(string strData)
{
double dblData;
try
{

dblData = Convert.ToDouble(strData);
if (dblData > 0)
{ return true; }
else
{ return false; }
}
catch
{
return false;
}
}

private bool IsValidNegativeNum(string strData)
{
double dblData;
try
{

dblData = Convert.ToDouble(strData);
if (dblData < 0)
{ return true; }
else
{ return false; }
}
catch
{
return false;
}
}

#endregion

#region override system method
public override string Text
{
get { return base.Text; }
set
{
if (IsValid(value, true))
base.Text = value;
}
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// trap Ctrl-V paste and prevent invalid values
// return false to allow further processing
if (keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns)
{
IDataObject iData = Clipboard.GetDataObject();

// assemble new string and check IsValid
string newText;
newText = base.Text.Substring(0, base.SelectionStart)
+ (string)iData.GetData(DataFormats.Text)
+ base.Text.Substring(base.SelectionStart + base.SelectionLength);

// check if data to be pasted is convertable to inputType
if (!IsValid(newText, true))
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

protected override void OnLeave(EventArgs e)
{
// handle - and leading zeros input since KeyPress handler must allow this
if (base.Text != "")
{
if (!IsValid(base.Text, false))
base.Text = "";
else if (Double.Parse(base.Text) == 0) // this used for -0, 000 and other strings
base.Text = "0";
}
base.OnLeave(e);
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
char c = e.KeyChar;
if (!Char.IsControl(c)) // not sure about this?? nothing in docs about what is Control char??
{
// prevent spaces
if (c.ToString() == " ")
{
e.Handled = true;
return;
}

string newText = base.Text.Substring(0, base.SelectionStart)
+ c.ToString() + base.Text.Substring(base.SelectionStart + base.SelectionLength);

if (!IsValid(newText, true))
e.Handled = true;
}
base.OnKeyPress(e);
}
#endregion

}





I define a column by inheritting the DataGridViewColumn, and add a property for this column, but in the design-mode
when I change the value of this property , the value can not be saved, ALl the codes as above.

thanks
AnswerRe: How to save the property of the user-defined column ? Pin
N a v a n e e t h6-Aug-09 18:07
N a v a n e e t h6-Aug-09 18:07 
Questionworking with webservices in .net Pin
prasadbuddhika6-Aug-09 17:12
prasadbuddhika6-Aug-09 17:12 
AnswerRe: working with webservices in .net Pin
N a v a n e e t h6-Aug-09 17:47
N a v a n e e t h6-Aug-09 17:47 
GeneralRe: working with webservices in .net Pin
prasadbuddhika6-Aug-09 19:27
prasadbuddhika6-Aug-09 19:27 
GeneralRe: working with webservices in .net Pin
N a v a n e e t h6-Aug-09 22:26
N a v a n e e t h6-Aug-09 22:26 
QuestionSend SMS /MMS to mobile phone from FTP server Pin
pearllyn6-Aug-09 15:46
pearllyn6-Aug-09 15:46 
AnswerRe: Send SMS /MMS to mobile phone from FTP server Pin
Spacix One6-Aug-09 17:28
Spacix One6-Aug-09 17:28 
AnswerRe: Send SMS /MMS to mobile phone from FTP server Pin
Blue_Boy7-Aug-09 3:41
Blue_Boy7-Aug-09 3:41 
QuestionSaving RSS feed in "utf-8" format. Pin
dbhalla_in6-Aug-09 14:29
dbhalla_in6-Aug-09 14:29 
AnswerRe: Saving RSS feed in "utf-8" format. Pin
Luc Pattyn6-Aug-09 14:45
sitebuilderLuc Pattyn6-Aug-09 14:45 
QuestionRelative URLs in Code-Behind Pin
Colin Pace6-Aug-09 10:52
Colin Pace6-Aug-09 10:52 
AnswerRe: Relative URLs in Code-Behind Pin
Adam R Harris6-Aug-09 11:07
Adam R Harris6-Aug-09 11:07 
QuestionC# Dataset bind to DataGridView update tabel in SQL Server 2005 DB Pin
Wheels0126-Aug-09 9:42
Wheels0126-Aug-09 9:42 
AnswerRe: C# Dataset bind to DataGridView update tabel in SQL Server 2005 DB Pin
Adam R Harris6-Aug-09 9:58
Adam R Harris6-Aug-09 9:58 
AnswerRe: C# Dataset bind to DataGridView update tabel in SQL Server 2005 DB Pin
Wheels01210-Aug-09 7:10
Wheels01210-Aug-09 7:10 
Question"Screenshot" from handle? Pin
dandy726-Aug-09 9:33
dandy726-Aug-09 9:33 
AnswerRe: "Screenshot" from handle? Pin
pelnor7-Aug-09 4:25
pelnor7-Aug-09 4:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.