VS2003里面,winform里面,获取用户编辑的目标单元格的行列索引,如何获取啊?

zibaozhangyu 2009-05-11 06:02:30
我现在要做一个管理页面,客户又要变态得要求用winform来实现类似EXCEL的功能了,特烦!!!

大概是这样的:
页面上有一个DataGrid,2003只有DataGrid没有View……

我现在想取得用户输入的单元格的信息,但是不知道用哪个事件来写。。
在2005里面很好实现,就是CELL[].column[]就好了
2003里面要怎么实现啊,烦死了!!!!!!高分求教!!!!
...全文
168 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zibaozhangyu 2009-05-12
  • 打赏
  • 举报
回复
谢谢楼上的,我马上去试试。

多谢!!
ai_li7758521 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ojlovecd 的回复:]
winform里的直接用dataGrid1[行索引,列索引]就行
[/Quote]支持
我姓区不姓区 2009-05-11
  • 打赏
  • 举报
回复
winform里的直接用dataGrid1[行索引,列索引]就行
xudongdong1990 2009-05-11
  • 打赏
  • 举报
回复
----DataGrid 樣板列的設置
<ItemTemplate>
<asp:RadioButton id=RadioButton2 runat="server" Enabled="False"
Checked=' <%# DataBinder.Eval(Container, "DataItem.Sex") %>'Text="男">
</asp:RadioButton>
<asp:RadioButton id=RadioButton1 runat="server" Enabled="False"
Checked=' <%# !(bool)DataBinder.Eval(Container, "DataItem.Sex") %>' Text="女">
</asp:RadioButton>
</ItemTemplate>
-----DataGrid 超連接列 ---連接到show.aspx頁面
<asp:HyperLinkColumn Text="點擊查看"
DataNavigateUrlField="StudentID"
DataNavigateUrlFormatString="Show.aspx?ID={0}"
DataTextField="StudentName" HeaderText="詳細信息">
</asp:HyperLinkColumn>

----DataGrid 導出Excel,在網頁上顯示Excel資料
private void btnMIME_Click(object sender, System.EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
int nCur = dgShow.CurrentPageIndex;
int nSize = dgShow.PageSize;
dgShow.AllowPaging = false;
BindData();
dgShow.Columns[7].Visible =false;
dgShow.RenderControl(hw);
dgShow.Columns[7].Visible =true;

//以下恢復分頁
dgShow.AllowPaging = true;
dgShow.CurrentPageIndex = nCur;
dgShow.PageSize = nSize;
BindData();
Response.Write(sw.ToString());
Response.End();
}

----格式化DataGrid 日期格式
<asp:BoundColumn DataField="Birthday" HeaderText="生日" DataFormatString="{0:yyyy-M-d}"> </asp:BoundColumn>
----DataGrid 實現 選擇刪除
選擇
public void CheckAll(object sender, System.EventArgs e)
{
CheckBox cbAll = (CheckBox)sender;
if(cbAll.Text=="全選")
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
cb.Checked = cbAll.Checked;
}
}
}
刪除
private void btnDelete_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
if(cb.Checked)
{
//以下執行刪除操作
int nID = int.Parse(dgi.Cells[0].Text);
string strSql = "delete from tbStudentinfo where studentid="+nID;
ExecuteSql(strSql);
}
}
dgShow.CurrentPageIndex = 0;
BindData();
}
Jock.Chen 2009-05-11
  • 打赏
  • 举报
回复
WinForm下DataGrid没见Items对象.
在DataGrid的CurrentCellChanged事件下添加代码就可以了.

private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{

int iRownr=this.dataGrid1.CurrentCell.RowNumber;
int iColnr=this.dataGrid1.CurrentCell.ColumnNumber;

object cellvalue1=this.dataGrid1[iRownr, iColnr];
object cellvalue2=null;
try
{
cellvalue2=this.dataGrid1[iRownr, iColnr+1];
this.textBox1.Text=cellvalue1.ToString()+" "+cellvalue2.ToString();
}
catch(Exception ex)
{
MessageBox.Show("the last column "+ex.Message,"Error");
cellvalue2=this.dataGrid1[iRownr, iColnr-1];
this.textBox1.Text=cellvalue2.ToString()+" "+cellvalue1.ToString();
}
}

zibaozhangyu 2009-05-11
  • 打赏
  • 举报
回复
1楼3楼4楼的。。。你们要不就是2005里面DataGridView,要不就是2003WEB上面的DataGrid。。

我要的是2003 winform里面的DataGrid。。。

我明天试试2楼说的

对了2楼,你那个能用

e. 点出来吗?
llsen 2009-05-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ojlovecd 的回复:]
dataGrid1.Items[行索引].Cells[列索引]
[/Quote]

对,是这样的
sharp_future 2009-05-11
  • 打赏
  • 举报
回复
--使用DataGrid 的編輯,刪除,操作功能
---Edit
private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = e.Item.ItemIndex;
BindData();
}
---cancel
private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex = -1;
BindData();
}
--delete
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(dgShow.Items.Count==1)
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
ExecuteSql(strSql);
BindData();
}
---update
private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strStudentID = e.Item.Cells[0].Text;//處于非編輯狀態
string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//處于編輯狀態
string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string strSex = ((CheckBox)(e.Item.Cells[3].FindControl("cbSex"))).Checked?"1":"0";
string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;

string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";

ExecuteSql(strSql);
dgShow.EditItemIndex = -1;
BindData();
}
--設置編輯框的格式
private void dgShow_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
for (int i=0;i<e.Item.Cells.Count;i++)
{
if(e.Item.Cells[i].Controls.Count>0)
{
try
{
TextBox t =(TextBox)e.Item.Cells[i].Controls[0];
t.Width=50;
}catch(Exception ee)
{
}
}
}
}
}

----DataGrid 樣板列的設置
<ItemTemplate>
<asp:RadioButton id=RadioButton2 runat="server" Enabled="False"
Checked='<%# DataBinder.Eval(Container, "DataItem.Sex") %>'Text="男">
</asp:RadioButton>
<asp:RadioButton id=RadioButton1 runat="server" Enabled="False"
Checked='<%# !(bool)DataBinder.Eval(Container, "DataItem.Sex") %>' Text="女">
</asp:RadioButton>
</ItemTemplate>
-----DataGrid 超連接列 ---連接到show.aspx頁面
<asp:HyperLinkColumn Text="點擊查看"
DataNavigateUrlField="StudentID"
DataNavigateUrlFormatString="Show.aspx?ID={0}"
DataTextField="StudentName" HeaderText="詳細信息">
</asp:HyperLinkColumn>

----DataGrid 導出Excel,在網頁上顯示Excel資料
private void btnMIME_Click(object sender, System.EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
int nCur = dgShow.CurrentPageIndex;
int nSize = dgShow.PageSize;
dgShow.AllowPaging = false;
BindData();
dgShow.Columns[7].Visible =false;
dgShow.RenderControl(hw);
dgShow.Columns[7].Visible =true;

//以下恢復分頁
dgShow.AllowPaging = true;
dgShow.CurrentPageIndex = nCur;
dgShow.PageSize = nSize;
BindData();
Response.Write(sw.ToString());
Response.End();
}

----格式化DataGrid 日期格式
<asp:BoundColumn DataField="Birthday" HeaderText="生日" DataFormatString="{0:yyyy-M-d}"></asp:BoundColumn>
----DataGrid 實現 選擇刪除
選擇
public void CheckAll(object sender, System.EventArgs e)
{
CheckBox cbAll = (CheckBox)sender;
if(cbAll.Text=="全選")
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
cb.Checked = cbAll.Checked;
}
}
}
刪除
private void btnDelete_Click(object sender, System.EventArgs e)
{
foreach(DataGridItem dgi in dgShow.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
if(cb.Checked)
{
//以下執行刪除操作
int nID = int.Parse(dgi.Cells[0].Text);
string strSql = "delete from tbStudentinfo where studentid="+nID;
ExecuteSql(strSql);
}
}
dgShow.CurrentPageIndex = 0;
BindData();
}
-----DataGrid 綁定自定義的下拉框進行編輯
1:<ItemTemplate>
<asp:DropDownList id="ddlSexI" runat="server" Enabled="False">
<asp:ListItem Value="1">男</asp:ListItem>
<asp:ListItem Value="0">女</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlSexE" runat="server">
<asp:ListItem Value="1">男</asp:ListItem>
<asp:ListItem Value="0">女</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>

2:dgShow.DataBind();
foreach(DataGridItem dgi in dgShow.Items)
{
//以下綁定非編輯狀態下拉列表
DropDownList ddI = (DropDownList)dgi.FindControl("ddlSexI");
if(ddI!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddI.SelectedIndex = 0;
else
ddI.SelectedIndex = 1;
}
//以下綁定編輯狀態下拉列表
DropDownList ddE = (DropDownList)dgi.FindControl("ddlSexE");
if(ddE!=null)
{
bool bSex = (bool)ds.Tables["studentinfo"].Rows[dgi.ItemIndex]["Sex"];
if(bSex)
ddE.SelectedIndex = 0;
else
ddE.SelectedIndex = 1;
}
3:
更新


-----DataGrid 實現自定義刪除鍵
創建
private void dgShow_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch(e.Item.ItemType)
{
case ListItemType.Item:
case ListItemType.EditItem:
case ListItemType.AlternatingItem:
Button myDeleteButton = (Button)e.Item.FindControl("btnDelete");
myDeleteButton.Text = "刪除此行";
myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要刪除第 " + e.Item.ItemIndex.ToString() + " 行嗎?');");
break;
}
}
刪除
private void dgShow_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="UserDelete")
dgShow_DeleteCommand(source,e);
}





---style.backgroundColor ---設置DataGrid一行的顏色
If e.Item.ItemType <> UI.WebControls.ListItemType.Header Then
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=""" + e.Item.Style("BACKGROUND-COLOR") + """")
e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor=""" + "#EFF3F7" + """")
End If
sharp_future 2009-05-11
  • 打赏
  • 举报
回复
下面是vs2003DataGrid 使用技巧,好好研究下。

//删除DataGrid 表格选定记录----

int intEmpId=(int)myDataGrid.DataKeys[e.Item.ItemIndex];

string deleteCmd="Delete from Employee where emp_id="+intEmpId.Tostring()+""



//点击表格打开

DatagriddataDateBound()

if(e.item.ItemType==ListItemType.Item||e.item.itemType==ListItemType.AlternatingItem)

e.item.attributes.add("onclick","window.open('Default.aspx?id="+e.item.Cells[0].Text+"');");



//双击表格连接到另一页

//在ItemDataBind事件中

if(e.item.ItemType==ListItemType.Item||e.item.itemType==ListItemType.AlternatingItem)

{

string OrderItemId=e.item.cells[1].Text;

e.item.Attributes.add("ondbclick","location.href='...'");

}

//双击表格打开新一页

if(e.item.ItemType==ListItemType.Item||e.item.itemType==ListItemType.AlternatingItem)

{

string OrderItemId=e,item.cells[1].Text;

e.item.attibute.add("ondbclick","open()");

}



//表格超级连接列传递参数-----------------------

<asp:HyperLinkColumn Target="_blank" headtext="ID号" DataTextField="id"

NavigateUrl="aaa.aspx?

id='<%# DataBinder.Eval(Container.DataItem,"数据字段1")%>' &name='<%# DataBind.Eval(Container.DataItem,"数据字段2")%>' />



//添加全选模板列

protect void checkall_checkchanged(object sender,system.eventargs e)

{

//改变列的选定,事先全选或全不选

CheckBox chkExport;

if(checkall.checked)

{

foreach(DataGridItem oDataGridItem in MyDataGrid.items)

{

chkExport=(CheckBox)oDataGridItem.FindControl("chkExport");

chkExport.Checked=true;

}

else

{

foreach(DataGridItem oDataGridItem in MyDataGrid.items)

{

chkExport=(CheckBox)oDataGridItem.FindControl("chkExport");

chkExport.Checked=false;

}

}

}

}





//编辑文本框的大小-----------

private void DataGrid1_ItemDataBound(obj sender,DataGridItemEventArgs e)

{

for(int i=0;i<e.item.Cells.Count-1;i++)

{

if(e.item.ItemType==ListItemType.EditType)

{

e.Item.Cells[i].Attributes.add("width","80px")

}

}

}





自定义分页--------------------------------

public static int pageCount;//总页面数

public static int curPageIndex=1;//当前页面

//下一页

if(DataGrid1.CurrentPageIndex<(DataGrid1.PageCount=1))

{

DataGrid1.CurrentPageIndex+=1;

curPageIndex+=1;

}

bind();//DataGrid1数据绑定函数



//上一页

if(DataGrid1.CurrentPageIndex>0)

{

DataGrid1.CurrentPageIndex+=1;

curPageIndex-=1;

}

bind();//DataGrid1数据绑定函数



//直接页面跳转

int a=int.Parse(JumpPage.Value.Trim());//JumpPage.Value.Trim()为跳转值

if(a<DataGrid1.PageCount)

{

this.DataGrid1.CurrentPageIndex=a;

}

bind();//DataGrid1数据绑定函数





//DataGrid 的删除提示

e.Item.Cells(1).Attributes("onclick") = "javascript:if (confirm('确定删除" + Trim(e.Item.Cells(2).Text) + "(" + Trim(e.Item.Cells(7).Text) + ")?')) {return true;} else {return false;};"

If e.Item.ItemType <> UI.WebControls.ListItemType.Header Then

e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=""" + e.Item.Style("BACKGROUND-COLOR") + """")

e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor=""" + "#EFF3F7" + """")

End If

If e.Item.ItemType = UI.WebControls.ListItemType.Item Or e.Item.ItemType = UI.WebControls.ListItemType.AlternatingItem Then

e.Item.Attributes.Add("onmouseover", "tdOver(this)")

e.Item.Attributes.Add("onmouseout", "tdOut(this)")

e.Item.Attributes.Add("onclick", "tdColor(this)")

e.Item.Attributes.Add("ondblclick", "tdColorDbl(this)")

End If



















df398286232 2009-05-11
  • 打赏
  • 举报
回复
datagrid1[datagrid1.currentRowIndex, 3] 单击好实现,双击麻烦
我姓区不姓区 2009-05-11
  • 打赏
  • 举报
回复
dataGrid1.Items[行索引].Cells[列索引]

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧